0

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();
}
Vikrant singh
  • 433
  • 1
  • 7
  • 25
  • Is that error message completed? because I can't find it in the source code. – JustWe Sep 18 '18 at 09:01
  • @Jiu still there. – Vikrant singh Sep 18 '18 at 09:07
  • I mean Qt source code, I only found https://github.com/qt/qtbase/blob/c5307203f5c0b0e588cc93e70764c090dd4c2ce0/src/widgets/kernel/qapplication.cpp#L130 – JustWe Sep 18 '18 at 09:08
  • Please edit your question to show the *exact* error message. These types of messages are usually the result of trying to create/initialize an instance of a `QObject` derived class *before* `main` has begun -- usually a static global of some description defined elsewhere. – G.M. Sep 18 '18 at 09:21
  • the error message is same which i have posted in the title. @G.M. . Okay, I will look into the code. – Vikrant singh Sep 18 '18 at 10:07

1 Answers1

0

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.

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56