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. Invoke a function after main
Forum Updated to NodeBB v4.3 + New Features

Invoke a function after main

Scheduled Pinned Locked Moved Solved General and Desktop
25 Posts 6 Posters 3.3k 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.
  • Christian EhrlicherC Christian Ehrlicher

    @jenya7 said in Invoke a function after main:

    How should I do it properly?

    For example with QTimer::singleShot()

    J Offline
    J Offline
    jenya7
    wrote on last edited by jenya7
    #4

    @Christian-Ehrlicher said in Invoke a function after main:

    @jenya7 said in Invoke a function after main:

    How should I do it properly?

    For example with QTimer::singleShot()

    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        QTimer::singleShot(600000, &app, SLOT(quit()));
        ...
        return app.exec();
    }
    

    It's executed before return app.exec(); and I need after.

    Wait a minute - 600000 - how do I know what time to set?
    app.exec() - it's a thread? what 's going on after? can do something in exec() ?

    1 Reply Last reply
    0
    • jsulmJ jsulm

      @jenya7 Really don't understand what is so difficult about it:

      int main(int argc, char *argv[])
      {
          uint32_t discovered = 0;
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
         //do some stuff
          auto result = a.exec();
          someFunction();
          return result;
      }
      

      Also, your title is wrong: you can't call anything after main...

      J Offline
      J Offline
      jenya7
      wrote on last edited by
      #5

      @jsulm said in Invoke a function after main:

      @jenya7 Really don't understand what is so difficult about it:

      int main(int argc, char *argv[])
      {
          uint32_t discovered = 0;
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
         //do some stuff
          auto result = a.exec();
          someFunction();
          return result;
      }
      

      Also, your title is wrong: you can't call anything after main...

      It goes to auto result = a.exec(); - then it goes out of main. I never get to someFunction();

      jsulmJ 1 Reply Last reply
      0
      • J jenya7

        @jsulm said in Invoke a function after main:

        @jenya7 Really don't understand what is so difficult about it:

        int main(int argc, char *argv[])
        {
            uint32_t discovered = 0;
            QApplication a(argc, argv);
            MainWindow w;
            w.show();
           //do some stuff
            auto result = a.exec();
            someFunction();
            return result;
        }
        

        Also, your title is wrong: you can't call anything after main...

        It goes to auto result = a.exec(); - then it goes out of main. I never get to someFunction();

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #6

        @jenya7 said in Invoke a function after main:

        It goes to auto result = a.exec(); - then it goes out of main. I never get to someFunction();

        How did you verify that?
        As soon as exec() terminates the flow continues with next statement.
        Maybe you app is crashing?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        J 1 Reply Last reply
        0
        • jsulmJ jsulm

          @jenya7 said in Invoke a function after main:

          It goes to auto result = a.exec(); - then it goes out of main. I never get to someFunction();

          How did you verify that?
          As soon as exec() terminates the flow continues with next statement.
          Maybe you app is crashing?

          J Offline
          J Offline
          jenya7
          wrote on last edited by
          #7

          @jsulm said in Invoke a function after main:

          @jenya7 said in Invoke a function after main:

          It goes to auto result = a.exec(); - then it goes out of main. I never get to someFunction();

          How did you verify that?
          As soon as exec() terminates the flow continues with next statement.
          Maybe you app is crashing?

          I set a break point on auto result = a.exec(); . The next step - break point vanished and I don't have the option to go on with break point. But I see GUI - it prints all the previous messages (till someFunction()) and all operates well. May be someFunction() was executed but it didn't print anything.

          J.HilkJ jsulmJ 2 Replies Last reply
          0
          • J jenya7

            @jsulm said in Invoke a function after main:

            @jenya7 said in Invoke a function after main:

            It goes to auto result = a.exec(); - then it goes out of main. I never get to someFunction();

            How did you verify that?
            As soon as exec() terminates the flow continues with next statement.
            Maybe you app is crashing?

            I set a break point on auto result = a.exec(); . The next step - break point vanished and I don't have the option to go on with break point. But I see GUI - it prints all the previous messages (till someFunction()) and all operates well. May be someFunction() was executed but it didn't print anything.

            J.HilkJ Online
            J.HilkJ Online
            J.Hilk
            Moderators
            wrote on last edited by J.Hilk
            #8

            @jenya7

            question:

            do you actually know what a.exec() is and does?

            and if you think you do, can you please post it here quickly ?

            I want to make sure, we're on the same page here


            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.

            J 1 Reply Last reply
            0
            • J jenya7

              @jsulm said in Invoke a function after main:

              @jenya7 said in Invoke a function after main:

              It goes to auto result = a.exec(); - then it goes out of main. I never get to someFunction();

              How did you verify that?
              As soon as exec() terminates the flow continues with next statement.
              Maybe you app is crashing?

              I set a break point on auto result = a.exec(); . The next step - break point vanished and I don't have the option to go on with break point. But I see GUI - it prints all the previous messages (till someFunction()) and all operates well. May be someFunction() was executed but it didn't print anything.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #9

              @jenya7 You apparently do not know what QApplication::exec() does (hint: read its documentation).
              Please read what @J-Hilk wrote...

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                @jenya7

                question:

                do you actually know what a.exec() is and does?

                and if you think you do, can you please post it here quickly ?

                I want to make sure, we're on the same page here

                J Offline
                J Offline
                jenya7
                wrote on last edited by jenya7
                #10

                @J-Hilk said in Invoke a function after main:

                @jenya7

                question:

                do you actually know what a.exec() is and does?

                and if you think you do, can you please post it here quickly ?

                I want to make sure, we're on the same page here

                That's what I'm tying to understand.
                Any way I could connect to some event like a.running() or something.

                jsulmJ JonBJ 2 Replies Last reply
                0
                • J jenya7

                  @J-Hilk said in Invoke a function after main:

                  @jenya7

                  question:

                  do you actually know what a.exec() is and does?

                  and if you think you do, can you please post it here quickly ?

                  I want to make sure, we're on the same page here

                  That's what I'm tying to understand.
                  Any way I could connect to some event like a.running() or something.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #11

                  @jenya7 What about reading https://doc.qt.io/qt-5/qapplication.html#exec ?

                  "Any way I could connect to some event like a.running() or something" - can you please explain what you actually want to do?
                  If you simply want to execute something after exec() then what I posted is the way to go...

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  J 1 Reply Last reply
                  3
                  • J jenya7

                    @J-Hilk said in Invoke a function after main:

                    @jenya7

                    question:

                    do you actually know what a.exec() is and does?

                    and if you think you do, can you please post it here quickly ?

                    I want to make sure, we're on the same page here

                    That's what I'm tying to understand.
                    Any way I could connect to some event like a.running() or something.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #12

                    @jenya7
                    As @jsulm says, what exactly is it you want to achieve?

                    If you are having trouble setting breakpoints after a.exec() but want to verify it is reached, why not put a qDebug() message in?

                    1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @jenya7 What about reading https://doc.qt.io/qt-5/qapplication.html#exec ?

                      "Any way I could connect to some event like a.running() or something" - can you please explain what you actually want to do?
                      If you simply want to execute something after exec() then what I posted is the way to go...

                      J Offline
                      J Offline
                      jenya7
                      wrote on last edited by
                      #13

                      @jsulm said in Invoke a function after main:

                      @jenya7 What about reading https://doc.qt.io/qt-5/qapplication.html#exec ?

                      "Any way I could connect to some event like a.running() or something" - can you please explain what you actually want to do?
                      If you simply want to execute something after exec() then what I posted is the way to go...

                      Yes but someFunction() not printing important messages.

                          auto result = a.exec();
                          someFunction();
                      

                      This way it does print but blocks GUI

                         someFunction();
                          auto result = a.exec();
                      
                      jsulmJ 1 Reply Last reply
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by Christian Ehrlicher
                        #14

                        @jenya7 said in Invoke a function after main:

                        This way it does print but blocks GUI
                        someFunction();

                        So why does someFunction() block? What are you doing inside this function? What's your final goal? What should this function do?

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

                        J 1 Reply Last reply
                        0
                        • J jenya7

                          @jsulm said in Invoke a function after main:

                          @jenya7 What about reading https://doc.qt.io/qt-5/qapplication.html#exec ?

                          "Any way I could connect to some event like a.running() or something" - can you please explain what you actually want to do?
                          If you simply want to execute something after exec() then what I posted is the way to go...

                          Yes but someFunction() not printing important messages.

                              auto result = a.exec();
                              someFunction();
                          

                          This way it does print but blocks GUI

                             someFunction();
                              auto result = a.exec();
                          
                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #15

                          @jenya7 Did you bother to read the link I gave you?
                          Probably not, so I will spend my time to explain: exec() is a BLOCKING call (it runs the Qt event loop). It blocks until you terminate the Qt event loop (this happens when you close the last window of your app). Then everything else after exec() is executed. So, expecting someFunction() to be executed after exec() WHILE your app is running is completely wrong! Also the break point after exec() will be hit when you close your app!

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • Christian EhrlicherC Christian Ehrlicher

                            @jenya7 said in Invoke a function after main:

                            This way it does print but blocks GUI
                            someFunction();

                            So why does someFunction() block? What are you doing inside this function? What's your final goal? What should this function do?

                            J Offline
                            J Offline
                            jenya7
                            wrote on last edited by
                            #16

                            @Christian-Ehrlicher said in Invoke a function after main:

                            @jenya7 said in Invoke a function after main:

                            This way it does print but blocks GUI
                            someFunction();

                            So why does your function block at all? What's your final goal? What should this function do?

                            someFunction(); - runs concurrent, in a separate thread, at least I hope it does. It goes through a list of remote devices and tries to connect (to discover).

                            Christian EhrlicherC 1 Reply Last reply
                            0
                            • J jenya7

                              @Christian-Ehrlicher said in Invoke a function after main:

                              @jenya7 said in Invoke a function after main:

                              This way it does print but blocks GUI
                              someFunction();

                              So why does your function block at all? What's your final goal? What should this function do?

                              someFunction(); - runs concurrent, in a separate thread, at least I hope it does. It goes through a list of remote devices and tries to connect (to discover).

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

                              @jenya7 said in Invoke a function after main:

                              someFunction(); - runs concurrent, in a separate thread, at least I hope it does. I

                              When it blocks then it does not.

                              And when you would have read and used my first answer, all your problems would have been gone... but I give up here.

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

                              J 1 Reply Last reply
                              0
                              • Christian EhrlicherC Christian Ehrlicher

                                @jenya7 said in Invoke a function after main:

                                someFunction(); - runs concurrent, in a separate thread, at least I hope it does. I

                                When it blocks then it does not.

                                And when you would have read and used my first answer, all your problems would have been gone... but I give up here.

                                J Offline
                                J Offline
                                jenya7
                                wrote on last edited by
                                #18

                                @Christian-Ehrlicher said in Invoke a function after main:

                                @jenya7 said in Invoke a function after main:

                                someFunction(); - runs concurrent, in a separate thread, at least I hope it does. I

                                When it blocks then it does not.

                                And when you would have read and used my first answer, all your problems would have been gone... but I give up here.

                                OK. But I have two questions

                                 QTimer::singleShot(600000, &app, SLOT(quit()));
                                

                                First - What time do I set - 600000? How long it takes to a.exec()?
                                Second - How do I put this

                                discovered =  m_sensor.DiscoverConcurrent(0, 0, SYS_DISC_MODE_FILE);
                                

                                Into this

                                 QTimer::singleShot(600000, &?,  ???);
                                
                                jsulmJ J.HilkJ 2 Replies Last reply
                                0
                                • J jenya7

                                  @Christian-Ehrlicher said in Invoke a function after main:

                                  @jenya7 said in Invoke a function after main:

                                  someFunction(); - runs concurrent, in a separate thread, at least I hope it does. I

                                  When it blocks then it does not.

                                  And when you would have read and used my first answer, all your problems would have been gone... but I give up here.

                                  OK. But I have two questions

                                   QTimer::singleShot(600000, &app, SLOT(quit()));
                                  

                                  First - What time do I set - 600000? How long it takes to a.exec()?
                                  Second - How do I put this

                                  discovered =  m_sensor.DiscoverConcurrent(0, 0, SYS_DISC_MODE_FILE);
                                  

                                  Into this

                                   QTimer::singleShot(600000, &?,  ???);
                                  
                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #19

                                  @jenya7 said in Invoke a function after main:

                                  What time do I set

                                  You can set 0, then the connected slot will be executed as soon as the event loop starts. Means: as soon as exec() starts.
                                  "How do I put this" - you do that in the slot connected to the timer...

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  J 1 Reply Last reply
                                  0
                                  • jsulmJ jsulm

                                    @jenya7 said in Invoke a function after main:

                                    What time do I set

                                    You can set 0, then the connected slot will be executed as soon as the event loop starts. Means: as soon as exec() starts.
                                    "How do I put this" - you do that in the slot connected to the timer...

                                    J Offline
                                    J Offline
                                    jenya7
                                    wrote on last edited by
                                    #20

                                    @jsulm said in Invoke a function after main:

                                    @jenya7 said in Invoke a function after main:

                                    What time do I set

                                    You can set 0, then the connected slot will be executed as soon as the event loop starts. Means: as soon as exec() starts.
                                    "How do I put this" - you do that in the slot connected to the timer...

                                    I see. QTimer::singleShot - it' s a widget and all widgets enabled by a.exec().

                                    jsulmJ artwawA 2 Replies Last reply
                                    0
                                    • J jenya7

                                      @jsulm said in Invoke a function after main:

                                      @jenya7 said in Invoke a function after main:

                                      What time do I set

                                      You can set 0, then the connected slot will be executed as soon as the event loop starts. Means: as soon as exec() starts.
                                      "How do I put this" - you do that in the slot connected to the timer...

                                      I see. QTimer::singleShot - it' s a widget and all widgets enabled by a.exec().

                                      jsulmJ Offline
                                      jsulmJ Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #21

                                      @jenya7 said in Invoke a function after main:

                                      QTimer::singleShot - it' s a widget

                                      QTimer::singleShot - sinse when it is a widget?! singleShot is a static method in the QTimer class, which is not a widget.

                                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      1 Reply Last reply
                                      3
                                      • J jenya7

                                        @Christian-Ehrlicher said in Invoke a function after main:

                                        @jenya7 said in Invoke a function after main:

                                        someFunction(); - runs concurrent, in a separate thread, at least I hope it does. I

                                        When it blocks then it does not.

                                        And when you would have read and used my first answer, all your problems would have been gone... but I give up here.

                                        OK. But I have two questions

                                         QTimer::singleShot(600000, &app, SLOT(quit()));
                                        

                                        First - What time do I set - 600000? How long it takes to a.exec()?
                                        Second - How do I put this

                                        discovered =  m_sensor.DiscoverConcurrent(0, 0, SYS_DISC_MODE_FILE);
                                        

                                        Into this

                                         QTimer::singleShot(600000, &?,  ???);
                                        
                                        J.HilkJ Online
                                        J.HilkJ Online
                                        J.Hilk
                                        Moderators
                                        wrote on last edited by
                                        #22

                                        @jenya7 said in Invoke a function after main:

                                        How long it takes to a.exec()?

                                        and here's your understanding problem.

                                        exec() returns, when only one thing happens

                                        • You tell your program to exit/stop running, one way or an other

                                        In the case of the operating system terminating your program prematurely - one way or another- you technically do not return from exec()

                                        inside that exec() call is a Qt styled "infinite" loop, an event loop.

                                        That allows signal slot and other event processing to occur. And because of that event processing, your app can react to keyboard mouse interactions etc and doesn't seem to "hang"

                                        If you now have somewhere an infinite loop yourself, that is called in one of those eventloopcycles, than the program "hangs" because eventloop can not proceed.

                                        So if your function blocks, it's not running in a separate thread, or the main thread has to wait for a return value, so it hangs anyways.


                                        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
                                        • J jenya7

                                          @jsulm said in Invoke a function after main:

                                          @jenya7 said in Invoke a function after main:

                                          What time do I set

                                          You can set 0, then the connected slot will be executed as soon as the event loop starts. Means: as soon as exec() starts.
                                          "How do I put this" - you do that in the slot connected to the timer...

                                          I see. QTimer::singleShot - it' s a widget and all widgets enabled by a.exec().

                                          artwawA Offline
                                          artwawA Offline
                                          artwaw
                                          wrote on last edited by
                                          #23

                                          @jenya7 said in Invoke a function after main:

                                          QTimer::singleShot - it' s a widget and all widgets enabled by a.exec().

                                          It would not kill you to check in the documentation before you start to talk rubbish you know.

                                          For more information please re-read.

                                          Kind Regards,
                                          Artur

                                          J 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