Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Move EVERY QObject to main thread and more...



  • 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



  • @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.



  • @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.



  • @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!


  • Qt Champions 2017

    @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());
    

Log in to reply