Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Don';我不明白为什么我能';t在QMainWindow子类中调用setCentralWidget_C++_Qt_Qt4 - Fatal编程技术网

C++ Don';我不明白为什么我能';t在QMainWindow子类中调用setCentralWidget

C++ Don';我不明白为什么我能';t在QMainWindow子类中调用setCentralWidget,c++,qt,qt4,C++,Qt,Qt4,//createActions(); //createMenu(); //createStatusBar() //loadSavedPosts(); //CheckForActionsNeed()//可能需要分解成更多功能 } 我得到的错误是: /usr/include/qt4/QtGui/qwidget.h::在构造函数“CLPoster::CLPoster()”中: /usr/include/qt4/QtGui/qwidget.h:787:错误:“qwidget::qwidget(cons


//createActions();
//createMenu();
//createStatusBar()

//loadSavedPosts(); //CheckForActionsNeed()//可能需要分解成更多功能 }

我得到的错误是:

/usr/include/qt4/QtGui/qwidget.h::在构造函数“CLPoster::CLPoster()”中: /usr/include/qt4/QtGui/qwidget.h:787:错误:“qwidget::qwidget(constqwidget&)”是私有的 /home/brett/projects/CLPoster/CLPoster build desktop/。/CLPoster/CLPoster.cpp:9:错误:在此上下文中 /home/brett/projects/CLPoster/CLPoster build desktop/。/CLPoster/CLPoster.cpp:10:错误:调用“CLPoster::setCentralWidget(QWidget&)”时没有匹配的函数 /usr/include/qt4/QtGui/qmainwindow.h:141:候选项为:void qmainwindow::setCentralWidget(QWidget*)
我无法解释错误消息。它说没有匹配的函数调用,但应该从QMainWindow继承它。它可能只是缺少C++的理解,而不是QT,我第一次使用它,但邓诺。谢谢您的帮助。

您不需要在堆上分配小部件并将指针传递给setCentralWidget()

函数要求您向QWidget(
QWidget*
)发送一个指针,而您正试图发送实际对象(或对对象的引用,
QWidget&
,正如编译器错误所暗示的那样)。如果您将中心小部件创建为指针(将成员函数更改为
QWidget*createCentralWidget()
),并将指针传递给setCentralWidget函数,您应该可以开始了

e、 g


然后在构造器中,您只需调用
setCentralWidget(createCentralWidget())
。QMainWindow析构函数将确保删除您的中心小部件。

所有QWidget项都必须在免费存储区(
new
)中分配,因为它们都必须有“父项”。在Qt中,父项将删除其子项(使用
delete
)。这就是为什么任何函数返回、接受小部件将在指向小部件的指针上执行的操作,而不是小部件本身;您需要执行相同的操作。

Arg,我的意思是创建一个指针(事实上,在过去已经做了很多次)但是停止了那个小的
*
。复制构造函数应该是我的线索。谢谢。 //clposter.h class CLPoster : public QMainWindow { Q_OBJECT

public: CLPoster();

private slots: QWidget createCentralWidget(); void createActions(); void createMenu(); void createStatusBar(); void loadSavedPosts(); }; //clposter.cpp CLPoster::CLPoster() { setWindowTitle("Craigslist Poster");

QWidget mainWidget = createCentralWidget();
setCentralWidget(mainWidget);
QWidget* CLPoster::createCentralWidget()
{
   QWidget* w = new QWidget;
   // Do stuff..
   return w;
}