Newbie - Confused about the central widget
Hi,
I've been reading through the GUI programming with Qt4 and really need help understanding the central widget. Here's my code:
Code:
#include <QMainWindow>
#include <QtGui>
{
public:
MainWindow(); // Constructor
};
MainWindow::MainWindow()
{
mainLayout->addWidget(spinBox);
this->centralWidget()->setLayout(mainLayout);
}
int main(int argc, char *argv[])
{
QApplication app
(argc, argv
);
// Create application. Arguments as parameters MainWindow *mainWindow = new MainWindow; // Create the application window
mainWindow->show(); // Show the application window
return app.exec();
}
This is my program simplified, all in one file. Although it compiles just fine, the program will crash upon being run.
I really have been trying to figure out how to do this correctly and would really appreciate if someone could point in the right direction!
Thanks :)
Re: Newbie - Confused about the central widget
you should first set the central widget because this call
this->centralWidget()
will return 0.
here the fix:
Code:
MainWindow::MainWindow()
{
//... use any widget you want.
// set the layout
mainLayout->addWidget(spinBox);
setCentralWidget(widget);
}