Using Qt mainwindow object separately or as class member of applications’ class

  application-design, c++, qt, user-interface

A theoretical question: in Qt examples, an object of QMainWindow class is always (at least in the examples I’ve seen so far) used to create a separate variable in main:

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    QMainWindow w;
    w.show();
    return app.exec();
}

I’m quite new to application development in general and C++ particularily, but to me it seems like having a mainwindow object as a member of app’s class might be more convenient to access main window’s methods from somewhere, something like that:

class MyApp: public QApplication
{
    QMainWindow* mainWindow;
    void someMethod();
}

int main(int argc, char **argv)
{
    MyApp app(argc, argv);
    QMainWindow mainWindow = new QMainWindow;
    app.mainWindow->show();
    return app.exec();
}

and then somewhere inside the app:

#define theApp (static_cast<MyApp*>(QCoreApplication::instance()))
theApp->mainWindow->someMethod();

Does it make sense, what are pros and cons of this approach?

Source: Windows Questions C++

LEAVE A COMMENT