Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Why modifying GUI from worker thread does not rise access violation
theknight47 last edited by
I tried to update a GUI from a worker thread and strangely no exception is caught and the GUI is modified normally. However, I have randomely some access violation from Qt5GUI.dll.
I want to know if that is possible ( that modifying a GUI from background thread may work ) and please explain me how the access violation occurs is this case.
Thank you in advance.
Yes it is possible. Nothing stops from you doing this. Can you tell me the run time error you hit when access violation happens ? It must be failing with 'sendEvent(...) method when you access the UI from other thread.
Modifying the GUI from a worker thread is not an access violation. However, it often causes data corruption. Qt's GUI classes are not "synchronized":http://doc.qt.io/qt-5/threads-synchronizing.html so it is not safe to modify them from different threads.
Your worker thread should do one of the following:
Emit a signal, to run a slot in the GUI thread, OR
Use QMetaObject::invokeMethod() to run a slot in the GUI thread
[quote author="Dheerendra" date="1425395696"]Yes it is possible. Nothing stops from you doing this.[/quote]No it's not. See the "documentation":http://doc.qt.io/qt-5/threads-qobject.html -- "GUI classes, notably QWidget and all its subclasses, are not reentrant. They can only be used from the main thread."