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. How to exit a Qt console app from an inner class? [ SOLVED ]

How to exit a Qt console app from an inner class? [ SOLVED ]

Scheduled Pinned Locked Moved General and Desktop
mainexitconsole applica
14 Posts 6 Posters 12.6k Views
  • 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.
  • S Offline
    S Offline
    SGaist
    Lifetime Qt Champion
    wrote on 19 Jun 2015, 23:02 last edited by
    #5

    But now you have to delete these two, in your case you can use:

    int main(int argc, char *argv[])
      {
          QCoreApplication app(argc, argv);
          InternetService service;
          QObject::connect(&service, SIGNAL(finished()), &app, SLOT(quit()));
          return app.exec();
      }
    

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    X 2 Replies Last reply 20 Jun 2015, 23:01
    0
    • S SGaist
      19 Jun 2015, 23:02

      But now you have to delete these two, in your case you can use:

      int main(int argc, char *argv[])
        {
            QCoreApplication app(argc, argv);
            InternetService service;
            QObject::connect(&service, SIGNAL(finished()), &app, SLOT(quit()));
            return app.exec();
        }
      
      X Offline
      X Offline
      xtingray
      wrote on 20 Jun 2015, 23:01 last edited by
      #6

      @SGaist Even better... thanks! :)


      Qt Developer

      1 Reply Last reply
      0
      • S SGaist
        19 Jun 2015, 23:02

        But now you have to delete these two, in your case you can use:

        int main(int argc, char *argv[])
          {
              QCoreApplication app(argc, argv);
              InternetService service;
              QObject::connect(&service, SIGNAL(finished()), &app, SLOT(quit()));
              return app.exec();
          }
        
        X Offline
        X Offline
        xtingray
        wrote on 2 Jul 2015, 21:21 last edited by xtingray 7 Feb 2015, 21:44
        #7

        @SGaist I made a little modification in my code but now it is not working and I can't figure out why?
        This is my situation:

        int main(int argc, char *argv[])
          {
              QCoreApplication app(argc, argv);
              InternetService service;
              QObject::connect(&service, SIGNAL(finished()), &app, SLOT(quit()));
              service.verifyEnv();
              return app.exec();
          }
        

        Just for testing, inside the verifyEnv() (public) method of the InternetService class, I only added this line:

        void InternetService::verifyEnv()
        {
            emit finished();
        }
        

        Nevertheless, the QCoreApplication object never quits. Why?


        Qt Developer

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 2 Jul 2015, 22:39 last edited by
          #8

          Got any message when starting the application in your terminal ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • A Offline
            A Offline
            ambershark
            wrote on 2 Jul 2015, 23:58 last edited by
            #9

            @xtingray In your new code that isn't working you are sending the signal emit finished() before you are in your event loop. So AFAIK that signal isn't caught once the loop starts.

            The loop i.e. app.exec() needs to be running before you call service.verifyEnv() and it's resulting emit finished().

            Since you are trying to exit right away by calling verifyEnv there is no need for the app.exec in that case, just return 0 from your main and never enter the loop.

            My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

            1 Reply Last reply
            0
            • X xtingray
              12 Jun 2015, 22:45

              Hi!
              This is my situation: I am running a TCP service from my main method. This service will be running forever unless a critical exception happen. Now, if an exception occurs I want to quit the whole application. Understanding that I can't use the Object::connect() method from the main() to call QCoreApplication::quit(), is there any accurate way to notify the app to finish the entire application in this context? I really appreciate any hint. Thanks.

                int main(int argc, char *argv[])
                {
                    QCoreApplication app(argc, argv);
                    InternetService service();
                    return app.exec();
                }
              
              A Offline
              A Offline
              alex_malyu
              wrote on 3 Jul 2015, 01:08 last edited by alex_malyu 7 Mar 2015, 01:13
              #10

              @xtingray

              in any case you you can call quit() from anywhere from your code

              just include
              #include <QCoreApplication>

              and call
              qApp->quit();

              1 Reply Last reply
              0
              • X Offline
                X Offline
                xtingray
                wrote on 3 Jul 2015, 14:28 last edited by xtingray 7 Mar 2015, 14:43
                #11

                First at all, thank you for all your suggestions. My comments:

                1. I tried to replace return app.exec() with return 0. The problem is that the program quits as far as I run it, even if I remove the signal instruction. As it is a service, I need it running all the time. I mean, the finished() signal is required but only for specific exceptions.
                2. I tried using the method qApp->quit(); too and surprisingly the application keeps running :O

                I decided to write a "Hello World" example, in case some of you want to help me to resolve this mystery:
                http://maefloresta.com/portal/files/test.zip

                The challenge is quite simple: If I run a first instance of the example, the service must keep running indefinitely. If I run a (concurrent) second instance, it has to quit as soon as it discovers that the port is busy.

                Anyone interested in to try it? Thanks.


                Qt Developer

                A 1 Reply Last reply 3 Jul 2015, 15:53
                0
                • X xtingray
                  3 Jul 2015, 14:28

                  First at all, thank you for all your suggestions. My comments:

                  1. I tried to replace return app.exec() with return 0. The problem is that the program quits as far as I run it, even if I remove the signal instruction. As it is a service, I need it running all the time. I mean, the finished() signal is required but only for specific exceptions.
                  2. I tried using the method qApp->quit(); too and surprisingly the application keeps running :O

                  I decided to write a "Hello World" example, in case some of you want to help me to resolve this mystery:
                  http://maefloresta.com/portal/files/test.zip

                  The challenge is quite simple: If I run a first instance of the example, the service must keep running indefinitely. If I run a (concurrent) second instance, it has to quit as soon as it discovers that the port is busy.

                  Anyone interested in to try it? Thanks.

                  A Offline
                  A Offline
                  aggregat
                  wrote on 3 Jul 2015, 15:53 last edited by
                  #12

                  @xtingray Make InternetService inherit from QObject and verifyEnv a slot. Then invoke the method as follows:

                  int main(int argc, char *argv[])
                  {
                  QCoreApplication app(argc, argv);
                  InternetService service;
                  QObject::connect(&service, SIGNAL(finished()), &app, SLOT(quit()));
                  QMetaObject::invokeMethod(&service, "verifyEnv", Qt::QueuedConnection);
                  return app.exec();
                  }

                  As an alternative, you could use the QtSingleApplication solution to restrict your application to a single instance only.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    ambershark
                    wrote on 5 Jul 2015, 03:45 last edited by
                    #13

                    I use QtSingleApplication for that stuff. It's probably better than just checking a port as you may want to do something if the port is busy. Just because a port is busy doesn't mean your service is actually running and is the one using it.

                    It could be another app. You could have permission issues and be unable to bind to that port. Or a host of other things.

                    My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                    1 Reply Last reply
                    0
                    • X Offline
                      X Offline
                      xtingray
                      wrote on 11 Jul 2015, 00:54 last edited by
                      #14

                      Finally, the best/clean way I found to deal with my requirement was this:

                      • Inside the main.cpp and before I start my service, I make all the verifications I need... if something goes wrong, then I call "return 0;" and that's it, a clean exit. If everything goes well, then I start my service. End of the story.

                      Thank you for your advices guys! :)


                      Qt Developer

                      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