Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Application cannot exit
Forum Updated to NodeBB v4.3 + New Features

Application cannot exit

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 9.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    panosk
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      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.

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dbzhang800
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • P Offline
          P Offline
          panosk
          wrote on last edited by
          #4

          [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.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mlong
            wrote on last edited by
            #5

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

            Software Engineer
            My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved