Application cannot exit



  • Hello all,

    I have created my first C++/Qt app (a plugin to an external application) which doesn't have a main gui. I just have to use QmessageBoxes in order to display various errors or misconfigurations to the user. As a result, I'm actually using QApplication although it is essentially a console app. The problem is that although the app is working correctly, it can't exit unless I place an exit(0) in the source file where the actual work is done. Also, if I display a "dummy" QMessageBox right after my app has done its work, clicking on the OK terminates it correctly. In other words, when something goes wrong and a message box appears, the app terminates as it should; when the app is doing its work without problems (thus, no message boxes appear), it can't exit :-).

    main.cpp is minimal:
    @
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Client SmallClient;
    return a.exec();
    }
    @

    Any advice or hints will be greatly appreciated.

    Thanks.


  • Moderators

    Whenever your process is finished, it should call qApp->quit(); to end the message loop.

    (qApp is always a reference to your current QApplication. You'll need to include QApplication in order to use it.)

    Actually, if there's no gui work being done, it should be sufficient to use QCoreApplication instead of QApplication.



  • Yes, it will keep running in such case.

    You has stared a event loop by a.exec(), which is a infinite loop. more or less like this

    @
    while(true){
    //...
    if (...)
    break;
    }
    @

    Normally, you should call a.exit() or convenient a.quit() to exit the event loop.

    However, if you have set the quitOnLastWindowClosed property of QApplication to true which is the default, a.exit() will be called when your last window is closed.

    Debao



  • [quote author="mlong" date="1337809876"]Whenever your process is finished, it should call qApp->quit(); to end the message loop.

    (qApp is always a reference to your current QApplication. You'll need to include QApplication in order to use it.)

    Actually, if there's no gui work being done, it should be sufficient to use QCoreApplication instead of QApplication.

    [/quote]

    Many thanks, including <QApplication> in the source file where the work is done and calling qApp->quit() terminates the app correctly. I can't use QCoreApplication because, as soon as a message tries to pop up, the app crashes with the message that "Cannot create a QWidget when no GUI is used", even though in my .pro file I have included gui (QT += gui) and configured as console (CONFIG +=console).

    Thank you.


  • Moderators

    Ah yes, my bad. I forgot that you were using message boxes, which do indeed require QApplication.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.