Move EVERY QObject to main thread and more...
-
Hey
I have a quite "complex" app and I wonder if Qt over any options to :
- Print every QObject in-app.
- Get each object thread
- 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
-
Hey
I have a quite "complex" app and I wonder if Qt over any options to :
- Print every QObject in-app.
- Get each object thread
- 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
QWidget
s. I asked a year ago whether there was something similar for allQObject
s instead and was told there is not. -
@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
QWidget
s. I asked a year ago whether there was something similar for allQObject
s 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. -
@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 theQObject
s 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
QWidget
s 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
QWidget
s 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! -
Hey
I have a quite "complex" app and I wonder if Qt over any options to :
- Print every QObject in-app.
- Get each object thread
- 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.
No. You can get all the children for a given root
QObject
though:object->findChildren<QObject *>()
- 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).- 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());