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 set up a slot for a console app using QCoreApplication?
Forum Updated to NodeBB v4.3 + New Features

How to set up a slot for a console app using QCoreApplication?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 5 Posters 1.5k Views 2 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.
  • GuerrianG Offline
    GuerrianG Offline
    Guerrian
    wrote on last edited by Guerrian
    #1

    I see that some people suggest using a slot so that the application can finish when using:

    class Runner: public QObject {
        Q_OBJECT
    public slots:
        void run();
    };
    
    void Runner::run()
    {
        // do application stuff here
    }
    
    int main(int argc, char *argv[])
    {
        QCoreApplication app(argc, argv);
        // how to set up a slot here to "run" the application?
        return app.exec();
    }
    

    I can't find a good example of this for a console application.

    Linux Mint 18.3
    Qt 5.14.1
    Qt Creator 4.11.1

    KroMignonK 1 Reply Last reply
    0
    • GuerrianG Guerrian

      I see that some people suggest using a slot so that the application can finish when using:

      class Runner: public QObject {
          Q_OBJECT
      public slots:
          void run();
      };
      
      void Runner::run()
      {
          // do application stuff here
      }
      
      int main(int argc, char *argv[])
      {
          QCoreApplication app(argc, argv);
          // how to set up a slot here to "run" the application?
          return app.exec();
      }
      

      I can't find a good example of this for a console application.

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by KroMignon
      #2

      @Guerrian Have you tried with something like Google? ==> http://treyweaver.blogspot.com/2013/02/qt-console-application-template-tutorial.html

      But to reply to your question:

      class Runner: public QObject {
          Q_OBJECT
      public slots:
          void run();
      };
      
      void Runner::run()
      {
          // do application stuff here
          ...
          // to quit application
          this->deleteLater();
      }
      
      int main(int argc, char *argv[])
      {
          QCoreAlippcation app(argc, argv);
      
          Runner run;
      
          // to start the runner object
          QTimer::singleShot(0, &run, &Runner::run);
      
         // to quit application on runner end
          QObject::connect(&run, &QObject::destroyed, &app, &QCoreApplication::quit);
          return app.exec();
      }
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      GuerrianG 1 Reply Last reply
      2
      • KroMignonK KroMignon

        @Guerrian Have you tried with something like Google? ==> http://treyweaver.blogspot.com/2013/02/qt-console-application-template-tutorial.html

        But to reply to your question:

        class Runner: public QObject {
            Q_OBJECT
        public slots:
            void run();
        };
        
        void Runner::run()
        {
            // do application stuff here
            ...
            // to quit application
            this->deleteLater();
        }
        
        int main(int argc, char *argv[])
        {
            QCoreAlippcation app(argc, argv);
        
            Runner run;
        
            // to start the runner object
            QTimer::singleShot(0, &run, &Runner::run);
        
           // to quit application on runner end
            QObject::connect(&run, &QObject::destroyed, &app, &QCoreApplication::quit);
            return app.exec();
        }
        
        GuerrianG Offline
        GuerrianG Offline
        Guerrian
        wrote on last edited by
        #3

        @KroMignon I get a compile error: undefined reference to `vtable for Runner'

        class Runner: public QObject {
            Q_OBJECT
        public:
            explicit Runner(QObject *parent = 0);
        public slots:
            void run();
        };
        
        Runner::Runner(QObject *parent): QObject(parent)
        {
        
        }
        
        void Runner::run()
        {
            cout << "hi" << endl;
        }
        

        Linux Mint 18.3
        Qt 5.14.1
        Qt Creator 4.11.1

        J.HilkJ 1 Reply Last reply
        0
        • GuerrianG Guerrian

          @KroMignon I get a compile error: undefined reference to `vtable for Runner'

          class Runner: public QObject {
              Q_OBJECT
          public:
              explicit Runner(QObject *parent = 0);
          public slots:
              void run();
          };
          
          Runner::Runner(QObject *parent): QObject(parent)
          {
          
          }
          
          void Runner::run()
          {
              cout << "hi" << endl;
          }
          
          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @Guerrian
          be gracious and gift Runner its own header on cpp files


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          2
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi
            The moc tools is only run on .h files
            so make sure anything having Q_OBJECT is in a .h

            Christian EhrlicherC 1 Reply Last reply
            3
            • mrjjM mrjj

              Hi
              The moc tools is only run on .h files
              so make sure anything having Q_OBJECT is in a .h

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @mrjj said in How to set up a slot for a console app using QCoreApplication?:

              The moc tools is only run on .h files

              No, moc also runs on .cpp when it finds an include in the form '#include "foo.moc"' (rerunning qmake is needed afaik) but it should be avoided

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              mrjjM 1 Reply Last reply
              4
              • Christian EhrlicherC Christian Ehrlicher

                @mrjj said in How to set up a slot for a console app using QCoreApplication?:

                The moc tools is only run on .h files

                No, moc also runs on .cpp when it finds an include in the form '#include "foo.moc"' (rerunning qmake is needed afaik) but it should be avoided

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Christian-Ehrlicher
                Yes i saw that in some of the samples but i consider it a hax :)

                ps. I love your new Tag !

                1 Reply Last reply
                2

                • Login

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