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. Create widgets in another thread
Forum Updated to NodeBB v4.3 + New Features

Create widgets in another thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 6 Posters 1.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.
  • bfbrmtB Offline
    bfbrmtB Offline
    bfbrmt
    wrote on last edited by
    #1

    I create some widgets in another thread and add them to window. All work fine.
    Many people told me that this is not allowed in qt.
    I wonder whether this is ok.
    My code: https://github.com/huangdade/CreateWidgetsInAnotherThread
    At beginning, I moved the widgets to main thread. But I found it still works without that.

    JKSHJ 1 Reply Last reply
    1
    • bfbrmtB bfbrmt

      I create some widgets in another thread and add them to window. All work fine.
      Many people told me that this is not allowed in qt.
      I wonder whether this is ok.
      My code: https://github.com/huangdade/CreateWidgetsInAnotherThread
      At beginning, I moved the widgets to main thread. But I found it still works without that.

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by JKSH
      #2

      @bfbrmt said in Create widgets in another thread:

      I create some widgets in another thread and add them to window. All work fine.
      Many people told me that this is not allowed in qt.

      No, it is not allowed in Qt. Widgets are not reentrant or thread-safe.

      But I found it still works

      It can work today and crash tomorrow.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      bfbrmtB 1 Reply Last reply
      1
      • C Offline
        C Offline
        ChrisW67
        wrote on last edited by
        #3

        @bfbrmt said in Create widgets in another thread:

        I create some widgets in another thread and add them to window. All work fine.

        Why? What problem do you think you are solving?

        bfbrmtB 1 Reply Last reply
        0
        • J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          you guys are over reacting, the @bfbrmt thinks he's creating QWidgets in an other thread. But he's calling the createWidgets function from the GUI thread. That means the createWidgets function itself is also executed in the GUI thread.

          Still, @bfbrmt try not to create QWidgets in any thread, but the one where your QApplication resides in.


          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
          1
          • bfbrmtB Offline
            bfbrmtB Offline
            bfbrmt
            wrote on last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • JKSHJ JKSH

              @bfbrmt said in Create widgets in another thread:

              I create some widgets in another thread and add them to window. All work fine.
              Many people told me that this is not allowed in qt.

              No, it is not allowed in Qt. Widgets are not reentrant or thread-safe.

              But I found it still works

              It can work today and crash tomorrow.

              bfbrmtB Offline
              bfbrmtB Offline
              bfbrmt
              wrote on last edited by
              #6

              @JKSH What if I move thoese widgets to main thread? Will it be safe?

              1 Reply Last reply
              0
              • C ChrisW67

                @bfbrmt said in Create widgets in another thread:

                I create some widgets in another thread and add them to window. All work fine.

                Why? What problem do you think you are solving?

                bfbrmtB Offline
                bfbrmtB Offline
                bfbrmt
                wrote on last edited by
                #7

                @ChrisW67 I needs to create many custom widgets and add them to a layout. The custom widget is complicated and the creation costs about 20ms for each. I want to create them in sub thread and move them to main thread. How about this idea?

                JonBJ JoeCFDJ 2 Replies Last reply
                0
                • bfbrmtB bfbrmt

                  @ChrisW67 I needs to create many custom widgets and add them to a layout. The custom widget is complicated and the creation costs about 20ms for each. I want to create them in sub thread and move them to main thread. How about this idea?

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

                  @bfbrmt said in Create widgets in another thread:

                  I want to create them in sub thread and move them to main thread. How about this idea?

                  You can't, as commented above. You must create/manipulate QWidgets only in the main, UI thread. Doesn't matter how much you "want" to do it in a secondary thread, you must not.

                  Creating a widget does not take 20ms. It is possible your computations for what you want in the widget might take time, but not the widget creation itself. If this is the case you must use signals & slots: the secondary thread can parcel up necessary non-QWidget information as it pleases, and then send that via a signal, with the main UI thread having a slot on that where it interprets the information to create an appropriate widget.

                  1 Reply Last reply
                  3
                  • bfbrmtB bfbrmt

                    @ChrisW67 I needs to create many custom widgets and add them to a layout. The custom widget is complicated and the creation costs about 20ms for each. I want to create them in sub thread and move them to main thread. How about this idea?

                    JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on last edited by JoeCFD
                    #9

                    @bfbrmt Just curious: what causes the creation of your widgets to take so long? If some works need to be done inside the creation, throw the works into a thread and create your widgets after they are done.

                    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