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.
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. -
@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.
-
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.
-
-
@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
QWidget
s 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.