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. call w.show() in another Thread
Forum Updated to NodeBB v4.3 + New Features

call w.show() in another Thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 216 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.
  • S Offline
    S Offline
    S_Hamzeh74
    wrote on last edited by
    #1

    Hi
    I Have a QtApplication and in the main.cpp
    I want to call w.show() function that opens the main window in another thread
    in fact, I want to open a window in a new thread
    how can I do that ?

    J.HilkJ 1 Reply Last reply
    0
    • S S_Hamzeh74

      Hi
      I Have a QtApplication and in the main.cpp
      I want to call w.show() function that opens the main window in another thread
      in fact, I want to open a window in a new thread
      how can I do that ?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @S_Hamzeh74 said in call w.show() in another Thread:

      how can I do that ?

      you can't

      ALL QWidget based classes have to live in the same thread as the QCore/QGui/QApplication

      you may connect a signal from a different thread to the show slot of a QWidget, but nothing more.


      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
      4
      • S Offline
        S Offline
        SimonSchroeder
        wrote on last edited by
        #3

        All GUI calls have to go through the main thread. You can, however, put the call to w.show() from another thread into the main thread. Using signals and slots was already mentioned. I personally don't like to write a whole bunch of signals and slots for this. What I do instead is to use QMetaObject::invokeMethod(qApp, [&w](){ w.show(); });. qApp will give you the instance of QApplication and invoke the lambda in the context (i.e. the thread) of qApp). You need to make sure that any work related to your window still executes inside another thread. I have put all my knowledge and different use cases particularly related to this question into a library: https://github.com/SimonSchroeder/QtThreadHelper

        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