Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. PyQt5 - How to create widgets on a thread then move it to the GUI thread?
Forum Updated to NodeBB v4.3 + New Features

PyQt5 - How to create widgets on a thread then move it to the GUI thread?

Scheduled Pinned Locked Moved Solved Qt for Python
14 Posts 5 Posters 2.6k Views 3 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.
  • P Offline
    P Offline
    Patitotective
    wrote on 22 Sept 2021, 11:51 last edited by Patitotective
    #1

    I'm working on an application and there is a long operation that I moved to a thread, but it stills get stuck because of when creating widgets based on the result of that long operation, so I moved the widgets creation to that thread so when it finishes I can grab that widget and display onto the main window.
    But I get:

    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QApplication(0x1f5e760), parent's thread is QThread(0x1cc7df0), current thread is QThread(0x2267a10)
    QObject::setParent: Cannot set parent, new parent is in a different thread
    

    So my question is: can I create somehow widgets that have NO parent so I can whenever I want set to them a parent on a different thread? There is another way of doing what I need? Or i CAN'T do it?

    J P 2 Replies Last reply 22 Sept 2021, 12:35
    0
    • P Patitotective
      22 Sept 2021, 11:51

      I'm working on an application and there is a long operation that I moved to a thread, but it stills get stuck because of when creating widgets based on the result of that long operation, so I moved the widgets creation to that thread so when it finishes I can grab that widget and display onto the main window.
      But I get:

      QObject: Cannot create children for a parent that is in a different thread.
      (Parent is QApplication(0x1f5e760), parent's thread is QThread(0x1cc7df0), current thread is QThread(0x2267a10)
      QObject::setParent: Cannot set parent, new parent is in a different thread
      

      So my question is: can I create somehow widgets that have NO parent so I can whenever I want set to them a parent on a different thread? There is another way of doing what I need? Or i CAN'T do it?

      J Offline
      J Offline
      JonB
      wrote on 22 Sept 2021, 12:35 last edited by JonB
      #2

      @Patitotective
      You cannot and should not either create or re-parent or use or whatever any widgets in any thread other than the main UI one. That's a general requirement in Qt. Whenever this is brought up the answer is always: you cannot do anything widget-wise in a sub-thread, all you can do is send signals to the main UI thread to get it to do what you would like. Even if there is some limited, contorted way to do it, I think others will ask for your use-case and advise an alternative sticking to the non-UI-widgets-in-thread principle.

      P 1 Reply Last reply 22 Sept 2021, 13:03
      2
      • J JonB
        22 Sept 2021, 12:35

        @Patitotective
        You cannot and should not either create or re-parent or use or whatever any widgets in any thread other than the main UI one. That's a general requirement in Qt. Whenever this is brought up the answer is always: you cannot do anything widget-wise in a sub-thread, all you can do is send signals to the main UI thread to get it to do what you would like. Even if there is some limited, contorted way to do it, I think others will ask for your use-case and advise an alternative sticking to the non-UI-widgets-in-thread principle.

        P Offline
        P Offline
        Patitotective
        wrote on 22 Sept 2021, 13:03 last edited by
        #3

        @JonB Thanks for the answer. I guess app users will have to wait some time.

        J 1 Reply Last reply 22 Sept 2021, 13:10
        0
        • P Patitotective
          22 Sept 2021, 13:03

          @JonB Thanks for the answer. I guess app users will have to wait some time.

          J Offline
          J Offline
          JonB
          wrote on 22 Sept 2021, 13:10 last edited by
          #4

          @Patitotective said in PyQt5 - How to create widgets on a thread then move it to the GUI thread?:

          I guess app users will have to wait some time.

          Not sure what you mean. If you use the standard technique of sending a signal from a processing thread to the UI thread, and have that do whatever in the way of creating & showing a widget, it should be perfectly responsive.

          P 1 Reply Last reply 30 Sept 2021, 23:41
          0
          • P Patitotective
            22 Sept 2021, 11:51

            I'm working on an application and there is a long operation that I moved to a thread, but it stills get stuck because of when creating widgets based on the result of that long operation, so I moved the widgets creation to that thread so when it finishes I can grab that widget and display onto the main window.
            But I get:

            QObject: Cannot create children for a parent that is in a different thread.
            (Parent is QApplication(0x1f5e760), parent's thread is QThread(0x1cc7df0), current thread is QThread(0x2267a10)
            QObject::setParent: Cannot set parent, new parent is in a different thread
            

            So my question is: can I create somehow widgets that have NO parent so I can whenever I want set to them a parent on a different thread? There is another way of doing what I need? Or i CAN'T do it?

            P Offline
            P Offline
            Pablo J. Rogina
            wrote on 22 Sept 2021, 13:16 last edited by
            #5

            @Patitotective said in PyQt5 - How to create widgets on a thread then move it to the GUI thread?:

            creating widgets based on the result of that long operation

            Couldn't you create most of the widgets in advance?
            And then when long operation is done just pick the required ones and maybe update labels/values on them?
            What that "pre-factory" approach save you some time when displaying "all the widgets"?
            But as @JonB mentioned, you may want to use signals from "long operation" back to GUI thread to keep it responsive as the background process moves on

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            P 1 Reply Last reply 24 Sept 2021, 15:21
            0
            • P Pablo J. Rogina
              22 Sept 2021, 13:16

              @Patitotective said in PyQt5 - How to create widgets on a thread then move it to the GUI thread?:

              creating widgets based on the result of that long operation

              Couldn't you create most of the widgets in advance?
              And then when long operation is done just pick the required ones and maybe update labels/values on them?
              What that "pre-factory" approach save you some time when displaying "all the widgets"?
              But as @JonB mentioned, you may want to use signals from "long operation" back to GUI thread to keep it responsive as the background process moves on

              P Offline
              P Offline
              Patitotective
              wrote on 24 Sept 2021, 15:21 last edited by
              #6

              @Pablo-J-Rogina The widgets to create are not on my control, they can be 10 or hundreds.

              P 1 Reply Last reply 24 Sept 2021, 18:07
              0
              • P Patitotective
                24 Sept 2021, 15:21

                @Pablo-J-Rogina The widgets to create are not on my control, they can be 10 or hundreds.

                P Offline
                P Offline
                Pablo J. Rogina
                wrote on 24 Sept 2021, 18:07 last edited by
                #7

                @Patitotective said in PyQt5 - How to create widgets on a thread then move it to the GUI thread?:

                they can be 10 or hundreds

                And you really think it's a good UX to have hundreds of widgets in the same screen?

                Upvote the answer(s) that helped you solve the issue
                Use "Topic Tools" button to mark your post as Solved
                Add screenshots via postimage.org
                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                P 1 Reply Last reply 24 Sept 2021, 20:20
                0
                • P Pablo J. Rogina
                  24 Sept 2021, 18:07

                  @Patitotective said in PyQt5 - How to create widgets on a thread then move it to the GUI thread?:

                  they can be 10 or hundreds

                  And you really think it's a good UX to have hundreds of widgets in the same screen?

                  P Offline
                  P Offline
                  Patitotective
                  wrote on 24 Sept 2021, 20:20 last edited by
                  #8

                  @Pablo-J-Rogina How can avoid displaying hundreds of widgets?
                  What I'm doing it's an API Reference creator (for Python modules) so I need to inspect a Python module that can have "infinite" members.
                  Any optimization recommendation is very welcomed.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 25 Sept 2021, 19:11 last edited by
                    #9

                    Hi,

                    Are you trying to implement a tool that generate diagrams like graphviz ?

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

                    P 1 Reply Last reply 25 Sept 2021, 19:19
                    0
                    • SGaistS SGaist
                      25 Sept 2021, 19:11

                      Hi,

                      Are you trying to implement a tool that generate diagrams like graphviz ?

                      P Offline
                      P Offline
                      Patitotective
                      wrote on 25 Sept 2021, 19:19 last edited by
                      #10

                      @SGaist PyAPIReference creates a tree of collapsible widgets that you can convert to a markdown file and edit it.
                      e.g.:
                      If you load this module:

                      version = "v1.0"
                      def say_hi(name: str, last_name: str) -> str:
                          print(f"Hi {name} {last_name}")
                      

                      You will get:
                      Screenshot from 2021-09-25 14-17-10.png

                      Then you can convert it to a markdown file and edit it to create your API Reference.

                      P 1 Reply Last reply 1 Oct 2021, 19:24
                      0
                      • J JonB
                        22 Sept 2021, 13:10

                        @Patitotective said in PyQt5 - How to create widgets on a thread then move it to the GUI thread?:

                        I guess app users will have to wait some time.

                        Not sure what you mean. If you use the standard technique of sending a signal from a processing thread to the UI thread, and have that do whatever in the way of creating & showing a widget, it should be perfectly responsive.

                        P Offline
                        P Offline
                        Patitotective
                        wrote on 30 Sept 2021, 23:41 last edited by Patitotective
                        #11

                        @JonB I think the part where my application get stuck is when creating widgets because in a thread I calculate some stuff, then using that stuff I create some widgets. So I don't see how it will get better if I send signals to create the widgets, I mean, I'm not doing anything else rather than creating widgets because I did already calculate the data.
                        Or are the Python for loops it selves freezing the app? And I should try to send signals to create the widgets?

                        jsulmJ 1 Reply Last reply 1 Oct 2021, 05:18
                        0
                        • P Patitotective
                          30 Sept 2021, 23:41

                          @JonB I think the part where my application get stuck is when creating widgets because in a thread I calculate some stuff, then using that stuff I create some widgets. So I don't see how it will get better if I send signals to create the widgets, I mean, I'm not doing anything else rather than creating widgets because I did already calculate the data.
                          Or are the Python for loops it selves freezing the app? And I should try to send signals to create the widgets?

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 1 Oct 2021, 05:18 last edited by
                          #12

                          @Patitotective As already said: do not create widgets in other threads than main/UI thread! This is simply not supported! Proper way to handle this is to emit signals from the other thread which are connected to slots in main thread. In these slots you can create the widgets.
                          "Or are the Python for loops it selves freezing the app?" - if those loops are in main thread and take long then yes, they block the Qt event loop. But with signals/slots you do not need such loops in main thread...

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

                          1 Reply Last reply
                          2
                          • P Patitotective
                            25 Sept 2021, 19:19

                            @SGaist PyAPIReference creates a tree of collapsible widgets that you can convert to a markdown file and edit it.
                            e.g.:
                            If you load this module:

                            version = "v1.0"
                            def say_hi(name: str, last_name: str) -> str:
                                print(f"Hi {name} {last_name}")
                            

                            You will get:
                            Screenshot from 2021-09-25 14-17-10.png

                            Then you can convert it to a markdown file and edit it to create your API Reference.

                            P Offline
                            P Offline
                            Pablo J. Rogina
                            wrote on 1 Oct 2021, 19:24 last edited by Pablo J. Rogina 10 Jan 2021, 19:24
                            #13

                            @Patitotective said in PyQt5 - How to create widgets on a thread then move it to the GUI thread?:

                            a tree of collapsible widgets

                            If I may suggest, why don't you create a list or table of all the function/methods for a particular module, and then if the user is interested in one row in particular, by double clicking it a dialog is displayed with all the information related to such method in the selected row.

                            This way "all the widgets" to create are reduce to just 10, maye 20 widgets, the ones needed to show information for "say_hi" in your example.

                            So the whole process is reduced to two parts: parsing the Python code and storing information for it (maybe a JSON model or a SQLite DB) and then displaying such information (using Model/View programming perhaps?)

                            Upvote the answer(s) that helped you solve the issue
                            Use "Topic Tools" button to mark your post as Solved
                            Add screenshots via postimage.org
                            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                            P 1 Reply Last reply 19 Oct 2021, 00:55
                            0
                            • P Pablo J. Rogina
                              1 Oct 2021, 19:24

                              @Patitotective said in PyQt5 - How to create widgets on a thread then move it to the GUI thread?:

                              a tree of collapsible widgets

                              If I may suggest, why don't you create a list or table of all the function/methods for a particular module, and then if the user is interested in one row in particular, by double clicking it a dialog is displayed with all the information related to such method in the selected row.

                              This way "all the widgets" to create are reduce to just 10, maye 20 widgets, the ones needed to show information for "say_hi" in your example.

                              So the whole process is reduced to two parts: parsing the Python code and storing information for it (maybe a JSON model or a SQLite DB) and then displaying such information (using Model/View programming perhaps?)

                              P Offline
                              P Offline
                              Patitotective
                              wrote on 19 Oct 2021, 00:55 last edited by
                              #14

                              @Pablo-J-Rogina I will try to only generate the first three layers of collapsible widgets, then if you click on a collapsible widget that is in depth 3 it generates its childen till the third layer again.

                              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