Getting error : Must construct a QGUIApplication first

2.6k views Asked by At

I am developing a GUI application, but whenever I am trying to close the application, it throws an error that "Must construct QGuiapplication first". My main is not returning exit code 0, so it's not exiting normally. I think some destructor is getting called twice but need some help here. I am attaching main.cpp code here for reference.

#include <QGuiApplication>
#include <QFontDatabase>
#include <QtWebEngine>

#include "ApplicationManager.h"
#include "AppLogger.h"

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QCoreApplication::setAttribute(Qt::AA_UseOpenGLES);

    QGuiApplication app(argc, argv);
    QtWebEngine::initialize();

    app.setApplicationName("MCS3.0");
    QFontDatabase::addApplicationFont(":/Fonts/Roboto.ttf");

#ifdef VERSION
    app.setApplicationVersion(QString("%1").arg(VERSION));
    logInfoMessage(app.applicationName()+app.applicationVersion()+" Started");
#endif

    ApplicationManager::instance().run();

    return app.exec();
}
1

There are 1 answers

0
Scheff's Cat On

The relevant part of the problem is inside ApplicationManager.h which was not exposed by OP.

I bet that it makes another instance of QApplication (or QGUIApplication or QCoreApplication).

How can I know this? It's partly a guess (as the name looks like) and partly result of the following test:

testQApp.cc:

#include <QtWidgets>

int main(int argc, char **argv)
{
  QApplication app(argc, argv);
  { QApplication app(argc, argv);
    QLabel qLbl("The app in app");
    qLbl.show();
    app.exec();
  }
  return app.exec();
}

testQApp.pro:

SOURCES = testQApp.cc

QT = widgets

Compiled and tested in cygwin64 on Windows 10:

$ qmake-qt5 testQApp.pro

$ make

$ ./testQApp 

snapshot of testQApp

When I quit the application, the issue occurs:

QApplication::exec: Please instantiate the QApplication object first
Segmentation fault (core dumped)

$

To make this complete, the relevant paragraph of doc. about QApplication:

For any GUI application using Qt, there is precisely one QApplication object, no matter whether the application has 0, 1, 2 or more windows at any given time. For non-QWidget based Qt applications, use QGuiApplication instead, as it does not depend on the QtWidgets library.

Please, note that the emphasize is not done by me.