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 avoid Qt app.exec() blocking main thread

How to avoid Qt app.exec() blocking main thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 5 Posters 13.4k 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
    student
    wrote on last edited by
    #1

    How to avoid Qt app.exec() blocking main thread?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What exactly is your use case ?

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

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

        Hi
        Its not blocking it
        its driving it. It makes all events go round and signal and slot works.

        So its 100% normal and needed for event driven app.

        But you dont have to use it. but without it. no gui.

        S 1 Reply Last reply
        3
        • SGaistS SGaist

          Hi,

          What exactly is your use case ?

          S Offline
          S Offline
          student
          wrote on last edited by
          #4

          @SGaist My use case is, I want to build an application which has GUI (using Qt), also has a command line, in the command line, I can type some commands, like > start_window, it will pop up my Qt window, but I still need the command line be available, I mean can type other commands. some of the commands may work on my window, some of them may not

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            In that case you should rather have two applications that interact with each other.

            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
            2
            • mrjjM mrjj

              Hi
              Its not blocking it
              its driving it. It makes all events go round and signal and slot works.

              So its 100% normal and needed for event driven app.

              But you dont have to use it. but without it. no gui.

              S Offline
              S Offline
              student
              wrote on last edited by
              #6

              @mrjj yeah, if I want gui, I must run app.exec. but one curious is, I see PyQt don't need the app.exec, we can just run window.show() will be fine. while in C++, it's a must. how does PyQt achieve this?

              mrjjM SGaistS JonBJ 3 Replies Last reply
              0
              • S student

                @mrjj yeah, if I want gui, I must run app.exec. but one curious is, I see PyQt don't need the app.exec, we can just run window.show() will be fine. while in C++, it's a must. how does PyQt achieve this?

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

                @student said in How to avoid Qt app.exec() blocking main thread:

                PyQt

                I guess it just makes a default one behind the scenes for you.

                Much of Qt needs event loop to function at all so there must be one.

                S 1 Reply Last reply
                0
                • S student

                  @mrjj yeah, if I want gui, I must run app.exec. but one curious is, I see PyQt don't need the app.exec, we can just run window.show() will be fine. while in C++, it's a must. how does PyQt achieve this?

                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @student PyQt also does as well as PySide2.

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

                  S 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    @student PyQt also does as well as PySide2.

                    S Offline
                    S Offline
                    student
                    wrote on last edited by
                    #9

                    @SGaist so how PySide does?

                    1 Reply Last reply
                    0
                    • mrjjM mrjj

                      @student said in How to avoid Qt app.exec() blocking main thread:

                      PyQt

                      I guess it just makes a default one behind the scenes for you.

                      Much of Qt needs event loop to function at all so there must be one.

                      S Offline
                      S Offline
                      student
                      wrote on last edited by
                      #10

                      @mrjj thanks. As I pasted above, I have a command line, So I am looking for a way to handle this.
                      thread may be one of the choice. I also want to see if there is an easy way.

                      1 Reply Last reply
                      0
                      • S student

                        @mrjj yeah, if I want gui, I must run app.exec. but one curious is, I see PyQt don't need the app.exec, we can just run window.show() will be fine. while in C++, it's a must. how does PyQt achieve this?

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

                        @student said in How to avoid Qt app.exec() blocking main thread:

                        I see PyQt don't need the app.exec, we can just run window.show() will be fine. while in C++, it's a must. how does PyQt achieve this?

                        Here is the program you described:

                        import sys
                        # from PySide2 import QtWidgets
                        from PyQt5 import QtWidgets
                        
                        if __name__ == '__main__':
                           app = QtWidgets.QApplication(sys.argv)
                           window = QtWidgets.QWidget()
                           window.show()
                        

                        with no app.exec(). Unsurprisingly, this does not " we can just run window.show() will be fine", it runs and exits, under both PyQt5 & PySide2. Could you show your code which has no app.exec_() yet runs fine?

                        S 1 Reply Last reply
                        2
                        • JonBJ JonB

                          @student said in How to avoid Qt app.exec() blocking main thread:

                          I see PyQt don't need the app.exec, we can just run window.show() will be fine. while in C++, it's a must. how does PyQt achieve this?

                          Here is the program you described:

                          import sys
                          # from PySide2 import QtWidgets
                          from PyQt5 import QtWidgets
                          
                          if __name__ == '__main__':
                             app = QtWidgets.QApplication(sys.argv)
                             window = QtWidgets.QWidget()
                             window.show()
                          

                          with no app.exec(). Unsurprisingly, this does not " we can just run window.show() will be fine", it runs and exits, under both PyQt5 & PySide2. Could you show your code which has no app.exec_() yet runs fine?

                          S Offline
                          S Offline
                          student
                          wrote on last edited by
                          #12

                          @JonB without app.exec, we can show the window in pythonwin.png

                          JonBJ 1 Reply Last reply
                          0
                          • S student

                            @JonB without app.exec, we can show the window in pythonwin.png

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

                            @student
                            Yes, that is not a running program. You have stopped in the Python interpreter with it on the screen. You could accomplish the same from C++ by running from a debugger and breaking after the .show(). So this does not illustrate anything about "not needing" app.exec(). So that's your answer to how "PyQt achieves it but C++ does not". PyQt/PySide2/Python are not behaving any differently compared to C++ in this respect.

                            S 1 Reply Last reply
                            1
                            • JonBJ JonB

                              @student
                              Yes, that is not a running program. You have stopped in the Python interpreter with it on the screen. You could accomplish the same from C++ by running from a debugger and breaking after the .show(). So this does not illustrate anything about "not needing" app.exec(). So that's your answer to how "PyQt achieves it but C++ does not". PyQt/PySide2/Python are not behaving any differently compared to C++ in this respect.

                              S Offline
                              S Offline
                              student
                              wrote on last edited by
                              #14

                              @JonB thanks, in the python interpreter, I can still type other command, and the window is also active, I can play with the window, I can also type command (interact with window) in python shell.
                              While in C++, assume I have the following function, and I compiled it into python, and in python, I run: start_window, it will not pop up.
                              If I open app.exec(), it can pop up, but the python shell (main thread) was blocked by window.
                              anything wrong I made here? Thanks

                              void start_window()
                              {
                              .....
                              // app.exec();
                              }

                              JonBJ 1 Reply Last reply
                              0
                              • S student

                                @JonB thanks, in the python interpreter, I can still type other command, and the window is also active, I can play with the window, I can also type command (interact with window) in python shell.
                                While in C++, assume I have the following function, and I compiled it into python, and in python, I run: start_window, it will not pop up.
                                If I open app.exec(), it can pop up, but the python shell (main thread) was blocked by window.
                                anything wrong I made here? Thanks

                                void start_window()
                                {
                                .....
                                // app.exec();
                                }

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

                                @student
                                I'm sorry, I can't keep going through this. In Qt, whether C++/Python/PyQt5/PySide2, you need to spin the main event loop via QApplication::exec() to have a proper running application.

                                and the window is also active, I can play with the window,

                                Show me how your way, with no app.exec(), you get the window to e.g. execute slots in response to signals raised, or raise signals which other windows you have opened get to execute their slots on?

                                If you can achieve all this/that you need without an app.exec() in Python/PyQt, then you are fine/best of luck.

                                I can also type command (interact with window) in python shell.

                                I don't know what "command" you can type, or what you count as "interact with window". What I think you want is to use the Python interpreter at this point (no idea why). I don't know how you can have on the one hand the full-fledged Python interpreter running and waiting for you type some Python into a shell, while at the same time having the UI operating properly without an app.exec(). You might achieve this by embedding a Python interpreter into the app for the user, but I don't see how you can do it from the invoking shell.

                                1 Reply Last reply
                                1
                                • KH-219DesignK Offline
                                  KH-219DesignK Offline
                                  KH-219Design
                                  wrote on last edited by
                                  #16

                                  Once upon a time I remember being confused about how GUI event loops work. It can initially be confusing and frustrating, then when something works it feels like black magic that still makes little sense.

                                  For someone interested in coming to a full understanding, I will make the observation/recommendation that "Game Engine Architecture" by Jason Gregory has a chapter on "the Game Loop" which is a good overview of the event-loop concept. (ISBN 1138035459)

                                  I'm not a game developer, but that book taught me a lot that I now use as a C++ application programmer.

                                  After you call "app.exec()", you would implement the rest of the interactions in "event handlers" (which in Qt are usually "slots"). Either way, these are functions that you write that will get called by the endless loop that runs inside "exec".

                                  "exec" is just an endless loop, as in:

                                  while (true) {
                                    processAllEvents();
                                  }
                                  

                                  The Qt framework gives you many ways to "hook into" the event-processing part. You do not need to add new threads to do this. You may, but it is optional.

                                  The concept really is too broad to tackle in one forum thread. Hopefully you can piece together enough clues from what all the respondents have said here.

                                  I would also highly recommend building some of the Qt "examples" projects. Search them for "QCoreApplication" to find command-line/console apps. I found this just now:

                                  qtbase/examples/dbus/complexpingpong/complexpong.cpp

                                  https://doc.qt.io/qt-5.9/qtdbus-complexpingpong-complexpong-cpp.html

                                  www.219design.com
                                  Software | Electrical | Mechanical | Product Design

                                  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