简体   繁体   中英

"Must construct a QApplication before a QWidget" error, but only on Windows builds?

I am working on a CMAKE C++ project which uses the QT Libraries. (For me, 5.15.3, for others 5.12.x)

In this project, there is a class Vtk3DViewer: public QWidget . In its constructor, it tries to create one of its member variables, which is of type QVTKOpenGLNativeWidget . This is from the VTK libraries. (Located in include\vtk-9.2\QVTKOpenGLNativeWidget.h)

For me, this new QVTKOpenGLNativeWidget() call within the constructor of my QWidget fails with the following error:

"Must construct a QApplication before a QWidget"

But that's just it, we do create a QApplication in main() well before this point. And this only happens on Windows . Linux builds appear to not have any issue.

Switching from Debug to RelWithDebugInfo moves the error - making it happen much earlier and on creating a QToolBarExt instead.

Why is this happening, and how do I fix it?

Here is an example of main() :

int main(int argc, char* argv[])
{
    // Set info for settings & registry
    QApplication::setOrganizationName(COMPANY_NAME);
    QApplication::setOrganizationDomain(COMPANY_DOMAIN);
    QApplication::setApplicationName(APP_NAME);

    // Set up for software-based backend for VTK
    QApplication::setAttribute(Qt::AA_UseOpenGLES);
    QApplication a(argc, argv);
    a.setWindowIcon(QIcon(":/main-window/favicon.ico"));

    // Instantiate singletons
    TaskExecutionManager::getInstance(); // Instantiate the task manager
    DataDispatcher::getInstance();

    // Create main window with default size
    MainWindow w;
    w.show();

    // Start application event loop
    return a.exec();
}

Then the main window's constructor calls:

void MainWindow::initializeMainWindow(Ui::MainWindow* ui)
{

    this->setDockOptions(AnimatedDocks | AllowNestedDocks | AllowTabbedDocks | GroupedDragging);
    // Main toolbar
    m_topToolBar = new QToolBarExt(this); // This causes a "Must construct a QApplication before a QWidget" error
}

The issue was that VTK was built in RELEASE, while our project was built in DEBUG.

(I did not see this as an issue, since we would never need to step into/debug VTK's code)

It appears this cryptic/incorrect error message will appear under these circumstances. Ensuring VTK and the project it includes are compiled the same way fixes it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM