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. QThread with multiple methods
QtWS25 Last Chance

QThread with multiple methods

Scheduled Pinned Locked Moved Unsolved General and Desktop
33 Posts 5 Posters 3.5k 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #8

    @JohnCu said in QThread with multiple methods:

    Please take look one more time

    You still not fixed my comments. DirectConnection is wrong!

    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
    1
    • Christian EhrlicherC Christian Ehrlicher

      @JohnCu said in QThread with multiple methods:

      Please take look one more time

      You still not fixed my comments. DirectConnection is wrong!

      J Offline
      J Offline
      JohnCu
      wrote on last edited by
      #9

      @Christian-Ehrlicher said in QThread with multiple methods:

      DirectConnection

      I understand, at first there was no connection specifier in this connect call. In previous post I described what happened then. So do you see any solution for mentioned problem?

      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #10

        @JohnCu said in QThread with multiple methods:

        So do you see any solution for mentioned problem?

        Don't use blocking and direct connections when working with threads. I don't see a problem calling a slot when a push button is pressed inside the thread which then triggers the execution.

        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
        1
        • Christian EhrlicherC Christian Ehrlicher

          @JohnCu said in QThread with multiple methods:

          So do you see any solution for mentioned problem?

          Don't use blocking and direct connections when working with threads. I don't see a problem calling a slot when a push button is pressed inside the thread which then triggers the execution.

          J Offline
          J Offline
          JohnCu
          wrote on last edited by
          #11

          @Christian-Ehrlicher The situation is as follows, I want to have my gui responsible all the time, but workers thread needs to emit signal to gui in order to plot the results. And the results needs to be ploted before subsequent step in workers thread. Now, data is plotted while app gets stucked on gui msgbox. I mean, when I dont use blocking and direct connections for update plot.

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #12

            @JohnCu said in QThread with multiple methods:

            And the results needs to be ploted before subsequent step in workers thread.

            But why? But I don't care - simply emit another signal once the plotting is done so your thread can continue to calculate.

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

            J 2 Replies Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

              @JohnCu said in QThread with multiple methods:

              And the results needs to be ploted before subsequent step in workers thread.

              But why? But I don't care - simply emit another signal once the plotting is done so your thread can continue to calculate.

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

              @Christian-Ehrlicher Ok, I will try. Thank you anyway, for all of your replies :)

              1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @JohnCu said in QThread with multiple methods:

                And the results needs to be ploted before subsequent step in workers thread.

                But why? But I don't care - simply emit another signal once the plotting is done so your thread can continue to calculate.

                J Offline
                J Offline
                JohnCu
                wrote on last edited by
                #14

                @Christian-Ehrlicher One more thing. I have menaged to handle stuff in workers thread (after emitting signal which initiates plotting, workers thread waits for succesful visualization), but there is still a problem in on_Run_triggered() method in App.cpp. After initiating result acquisition (emit onRun();) the main thread also needs to wait till operation is finished. Moreover, gui needs to be responsible all the time. When i create here a loop, which waits for signal from
                workers class, the gui thread is stucked in this loop and visualization in gui is impossible. Are there any methods which, will enable me to have responsible thread (in order to plot according to signal from worker), but paused on emit onRun() line?

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #15

                  Show a modal wait dialog.

                  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
                  1
                  • Christian EhrlicherC Christian Ehrlicher

                    Show a modal wait dialog.

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

                    @Christian-Ehrlicher But does not a modal wait dialog assume, that everything, except this dialog window is unable to be clicked? I want to achieve both gui responsibility and live data visualization.
                    For now I have a following loop:

                    {
                    	QEventLoop loop;
                    	loop.connect(&workers_obj, SIGNAL(on_ready()), SLOT(quit()));
                    	QFuture<void> future;
                    	future = QtConcurrent::run(&this->workers_obj, &Worker::acquire_data);
                    	loop.exec();
                    }
                    

                    Here I use on_ready() signal generated at the end of acquire_data() method. Everything works fine, until I want to cancel computation in worker thread. It leads to null-pointer access etc.

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #17

                      You wanted to wait in the main dialog (for whatever reason) - I told you a solution.
                      Now you tell me you won't block the ui - so don't block it and connect a slot to the finished signal of the thread.

                      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
                      1
                      • Christian EhrlicherC Christian Ehrlicher

                        You wanted to wait in the main dialog (for whatever reason) - I told you a solution.
                        Now you tell me you won't block the ui - so don't block it and connect a slot to the finished signal of the thread.

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

                        @Christian-Ehrlicher Ok, let me explain the situation one more time. I launch a function from a gui thread (gui class code). Then i need to wait for the function to finish its job, before i go to a subsequent line. However, mu gui thread event loop needs to be active all the time, in order to enable data plotting. I can not freeze gui thread, I need to have it responsible, but it needs to be waiting in a line of launching function from workers object. There should be something like "do subsequent line, but first perform action prompted by an indicated signal". For now it is handled in a loop mentioned before, but I know, that it is not an optimal solution...

                        1 Reply Last reply
                        0
                        • Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #19

                          Again: connect a signal which is emitted from the thread when the first part is finished. Connect this signal to a slot in the gui thread which does the work you need to do in the gui thread which then emits another signal to do the next work in the thread.

                          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
                          1
                          • Christian EhrlicherC Christian Ehrlicher

                            Again: connect a signal which is emitted from the thread when the first part is finished. Connect this signal to a slot in the gui thread which does the work you need to do in the gui thread which then emits another signal to do the next work in the thread.

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

                            @Christian-Ehrlicher Yes, I added such signal. But how should I check if the signal was emitted? I can not do a while loop, which constantly checks whether signal was emitted, because gui thread will be locked in such loop. Maybe you meant something else?

                            J.HilkJ 1 Reply Last reply
                            0
                            • J JohnCu

                              @Christian-Ehrlicher Yes, I added such signal. But how should I check if the signal was emitted? I can not do a while loop, which constantly checks whether signal was emitted, because gui thread will be locked in such loop. Maybe you meant something else?

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

                              @JohnCu you don't need to check that yourself

                              simply connect a slot to the signal, as soon as it is emitted and your main application is in no loop (infinite loop for example) the slot will be called!


                              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
                              1
                              • J.HilkJ J.Hilk

                                @JohnCu you don't need to check that yourself

                                simply connect a slot to the signal, as soon as it is emitted and your main application is in no loop (infinite loop for example) the slot will be called!

                                J Offline
                                J Offline
                                JohnCu
                                wrote on last edited by
                                #22

                                @J-Hilk Yes, but my workers thread computation are a little bit time consuming, and I need to be sure, that before going to subsequent line in gui code (just after launching computation on workers thread) the computations are done. Your approach would work, if there is no more code in function, inside which workers thread method is launched.

                                J.HilkJ 1 Reply Last reply
                                0
                                • J JohnCu

                                  @J-Hilk Yes, but my workers thread computation are a little bit time consuming, and I need to be sure, that before going to subsequent line in gui code (just after launching computation on workers thread) the computations are done. Your approach would work, if there is no more code in function, inside which workers thread method is launched.

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

                                  @JohnCu
                                  I don't see the problem, YOU decide when the signal is emitted inside your code, don't you?

                                  Emit the allDone signal when you're all done, and not before


                                  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
                                  1
                                  • J.HilkJ J.Hilk

                                    @JohnCu
                                    I don't see the problem, YOU decide when the signal is emitted inside your code, don't you?

                                    Emit the allDone signal when you're all done, and not before

                                    J Offline
                                    J Offline
                                    JohnCu
                                    wrote on last edited by
                                    #24

                                    @J-Hilk Yes, of course, there is no problem with emitting a signal from workers thread, I do it when computations are done. But, take look at this code from gui class:

                                    emit onConnecttoHardware();
                                    
                                    // if connected to hardware
                                    QMessageBox::information(this, "Message", "Hardware connected");
                                    

                                    First i launch action, then i show, that it is finished. however, after emitting a signal to start the action, the main thread goes straight to show QMessageBox, which can not be prompted before computations are done... I need to wait at the first line of this code to obtain allDone signal and have my gui responsible while waiting, because i need to plot some results, which are also prompted by a signal emitted from workers thread. If I do as you suggested, main thread displays messagebox immediately, before action finished (allDone signal is emitted)...

                                    aha_1980A 1 Reply Last reply
                                    0
                                    • J JohnCu

                                      @J-Hilk Yes, of course, there is no problem with emitting a signal from workers thread, I do it when computations are done. But, take look at this code from gui class:

                                      emit onConnecttoHardware();
                                      
                                      // if connected to hardware
                                      QMessageBox::information(this, "Message", "Hardware connected");
                                      

                                      First i launch action, then i show, that it is finished. however, after emitting a signal to start the action, the main thread goes straight to show QMessageBox, which can not be prompted before computations are done... I need to wait at the first line of this code to obtain allDone signal and have my gui responsible while waiting, because i need to plot some results, which are also prompted by a signal emitted from workers thread. If I do as you suggested, main thread displays messagebox immediately, before action finished (allDone signal is emitted)...

                                      aha_1980A Offline
                                      aha_1980A Offline
                                      aha_1980
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #25

                                      @JohnCu said in QThread with multiple methods:

                                      I need to wait at the first line of this code to obtain allDone signal and have my gui responsible while waiting,

                                      And this, you have to understand, is a discrepance.

                                      You cannot wait in GUI threads.

                                      What you probably want to do is, to disallow user input until the allDone signal is received. And that's how it's usually done in professional apps.

                                      You most likely also want your user to be able to abort the process, in case something goes wrong. All that is impossible if the GUI thread stucks.

                                      Regards

                                      Qt has to stay free or it will die.

                                      1 Reply Last reply
                                      2
                                      • J.HilkJ Online
                                        J.HilkJ Online
                                        J.Hilk
                                        Moderators
                                        wrote on last edited by
                                        #26

                                        @JohnCu

                                        add the following signals to your worker

                                        signals:
                                        	void on_enable_buttons();
                                        	void update_plot();
                                        //new
                                               void onHardwareConnected();
                                               void onOperationSuccesfull
                                        

                                        Connects in your App.cpp

                                        connect(&worker, &Worker:: onHardwareConnected, this, App:: onRun);
                                        connect(&worker, &Worker:: onHardwareConnected, this, [=]()->void{QMessageBox::information(this, "Message", "Hardware connected");});
                                        connect(&worker, &Worker:: onOperationSuccesfull, this, [=]()->void{
                                             QMessageBox::information(this, "Message", "Operation succesful");
                                             enable_all_buttons();
                                        });
                                        
                                        void App::on_Run_triggered()
                                        {
                                        	disable_all_buttons();
                                                emit onConnecttoHardware();
                                        	QMessageBox::information(this, "Message", "Prepare hardware");
                                        }
                                        

                                        something like this could work


                                        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 Offline
                                          J Offline
                                          JohnCu
                                          wrote on last edited by JohnCu
                                          #27

                                          @aha_1980 said in QThread with multiple methods:

                                          @JohnCu said in QThread with multiple methods:

                                          I need to wait at the first line of this code to obtain allDone signal and have my gui responsible while waiting,

                                          And this, you have to understand, is a discrepance.

                                          You cannot wait in GUI threads.

                                          What you probably want to do is, to disallow user input until the allDone signal is received. And that's how it's usually done in professional apps.

                                          You most likely also want your user to be able to abort the process, in case something goes wrong. All that is impossible if the GUI thread stucks.

                                          Regards

                                          Yes, that is exactly what I want. I know, that I cannot block a GUI thread. So how would you solve this problem? I just want this thread to respond for incoming signals, which will finally allow to step it to subsequent line. I want to tell compiler "stay here, process incoming signals and continue to subsequent line (within this method), as soon as indicated signal will be emitted"

                                          @aha_1980 said in QThread with multiple methods:

                                          @JohnCu said in QThread with multiple methods:

                                          I need to wait at the first line of this code to obtain allDone signal and have my gui responsible while waiting,

                                          And this, you have to understand, is a discrepance.

                                          You cannot wait in GUI threads.

                                          What you probably want to do is, to disallow user input until the allDone signal is received. And that's how it's usually done in professional apps.

                                          You most likely also want your user to be able to abort the process, in case something goes wrong. All that is impossible if the GUI thread stucks.

                                          Regards

                                          Thanks, it would work if there is only one function called from on_Run_triggered() method, but there are more:

                                          void App::on_Run_triggered()

                                          {
                                          disable_all_buttons();
                                          QMessageBox::information(this, "Message", "Prepare hardware");

                                          emit onConnecttoHardware();
                                          
                                          // if connected to hardware
                                          QMessageBox::information(this, "Message", "Hardware connected");
                                          
                                          emit onRun();
                                          		
                                          QMessageBox::information(this, "Message", "Operation succesful");
                                          

                                          }

                                          The problem is, that I cannot emit onRun() before I am not sure if hardware was connected properly... Based on your responses, the only way to make it work (besides QEventLoop, that I mentioned before, however it is not safe) would be to split on_Run_triggered() method and place rest of the code into another method, which would be called via slot after getting allDone signal? So after calling first method, a main gui thread will exit on_Run_triggered() and will be waiting for subsequent signal emissions i.e. allDone. am I right?

                                          I think, a similar problem was described here: https://stackoverflow.com/questions/3556421/blocked-waiting-for-a-asynchronous-qt-signal

                                          J.HilkJ 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