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. Move EVERY QObject to main thread and more...

Move EVERY QObject to main thread and more...

Scheduled Pinned Locked Moved Unsolved General and Desktop
qthreadqobject
5 Posts 3 Posters 2.2k Views 2 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.
  • D Offline
    D Offline
    Dariusz
    wrote on last edited by
    #1

    Hey

    I have a quite "complex" app and I wonder if Qt over any options to :

    1. Print every QObject in-app.
    2. Get each object thread
    3. Move object to the main thread.

    Its a kind of "sanity" check as I recently discovered that I was running half of my app in another thread "by accident..."

    TIA

    JonBJ kshegunovK 2 Replies Last reply
    0
    • D Dariusz

      Hey

      I have a quite "complex" app and I wonder if Qt over any options to :

      1. Print every QObject in-app.
      2. Get each object thread
      3. Move object to the main thread.

      Its a kind of "sanity" check as I recently discovered that I was running half of my app in another thread "by accident..."

      TIA

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @Dariusz said in Move EVERY QObject to main thread and more...:

      Print every QObject in-app.

      For debugging I used https://doc.qt.io/qt-5/qapplication.html#allWidgets to walk all QWidgets. I asked a year ago whether there was something similar for all QObjects instead and was told there is not.

      D 1 Reply Last reply
      0
      • JonBJ JonB

        @Dariusz said in Move EVERY QObject to main thread and more...:

        Print every QObject in-app.

        For debugging I used https://doc.qt.io/qt-5/qapplication.html#allWidgets to walk all QWidgets. I asked a year ago whether there was something similar for all QObjects instead and was told there is not.

        D Offline
        D Offline
        Dariusz
        wrote on last edited by
        #3

        @JonB Gotta say having QObject list for entire app would be a great help. Not sure how large companies do it but if I had threading done and coders were adding modules, this could help debug & validate if all modules run in correct threads/etc.
        In any case thank you for the widgets one. This will definitely help.

        JonBJ 1 Reply Last reply
        0
        • D Dariusz

          @JonB Gotta say having QObject list for entire app would be a great help. Not sure how large companies do it but if I had threading done and coders were adding modules, this could help debug & validate if all modules run in correct threads/etc.
          In any case thank you for the widgets one. This will definitely help.

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #4

          @Dariusz
          Yeah, can't help you on the QObjects one, I wanted access to that similarly for debugging and (like you) couldn't find anything so I asked, I either received no response or got a definite "no". I don't doubt there is some internal table/hierarchy but if so it doesn't seem to be exposed.

          For the QWidgets one, it;s so old I can't locate my post, but I wrote there what I am using (from Python). I walked the tree looking for "orphans", i.e. stuff which even from full descent from top-level (QMainWindow assuming you have only one, something like the following would do:

          QMainWindow* getMainWindow()
          {
              foreach (QWidget *w, qApp->topLevelWidgets())
                  if ((QMainWindow* mainWin = qobject_cast<QMainWindow*>(w)))
                      return mainWin;
              return nullptr;
          }
          

          Given that you can work out if any QWidgets anywhere have no parentage, if they are not top-level/modeless themselves then they (and all their descendants) are orphaned and leaking, surprising what you may be able to spot this way!

          1 Reply Last reply
          0
          • D Dariusz

            Hey

            I have a quite "complex" app and I wonder if Qt over any options to :

            1. Print every QObject in-app.
            2. Get each object thread
            3. Move object to the main thread.

            Its a kind of "sanity" check as I recently discovered that I was running half of my app in another thread "by accident..."

            TIA

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #5

            @Dariusz said in Move EVERY QObject to main thread and more...:

            1. Print every QObject in-app.

            No. You can get all the children for a given root QObject though: object->findChildren<QObject *>()

            1. Get each object thread

            QObject::thread() for the object's associated thread (i.e. depending on its affinity), QThread::currentThread() for the thread object associated with the current context (and yes, these can differ).

            1. Move object to the main thread.

            Only from the thread the QObject belongs to, no other. QObject instances can only be "pushed" into another thread from their own thread, but not "pulled" from their thread from another.
            To move the object, you do what you usually do:

            object->moveToThread(QCoreApplication::instance()->thread());
            

            Read and abide by the Qt Code of Conduct

            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