I am just curious if there are any gotcha's to creating widgets on the stack? In the QT examples I've looked at, typically the main function looks like this:
int main(int argc, char** argv)
{
QApplication app(argc, argv);
MainWindow mw(0, "mainwindow");
if (mw.Initialize())
{
app.setMainWidget(&mw);
mw.show();
return app.exec();
}
return 0;
}
Here, the instance of my MainWindow is created on the stack. However, the widgets that make up the MainWindow are all allocated on the heap using new. A typical header looks something like this:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget* pParent=0, const char* pInternalName=0, WFlags Flags=WType_TopLevel);
~MainWindow();
private:
QMenuBar* mp_MenuBar;
QWidget* mp_CentralWidget;
QVGroupBox* mp_ControlPane;
StatusPane* mp_StatusPane;
...
You can see that pointers to widgets are used in the header -- but why? If the MainWindow widget is going to create a static display, why not declare them as actuals objects and simply construct them in the initializer list for the MainWindow constructor?
Bookmarks