PDA

View Full Version : Widget access from a QThread



spike6479
30th September 2020, 15:18
I understand that you can't access gui widgets from a thread. But I have found that I can call setText for widgets (QPushButton, QLabel etc) from a thread and it seems to work. Is this by design and I can count on it? Or should I go through the hassle of creating signal/slot to do this?

Thanks much.

d_stranz
30th September 2020, 15:59
AFAIK, there is no rule against accessing QObject-based instances from threads (if there was, the entire cross-thread signal/slot protocol would fall apart).

What you can't do is create any QWidget-based objects anywhere except the main thread (where QApplication lives) and you can't perform any painting operations directly on them from another thread. You can do things like draw on off-screen paint devices (like QImage) and pass them back to the main thread for display.

So calling methods that set QWidget properties should in general be OK since any GUI updates that occur as a result will be done through the event loop running in the main thread.

ChristianEhrlicher
30th September 2020, 17:59
AFAIK, there is no rule against accessing
So calling methods that set QWidget properties should in general be OK since any GUI updates that occur as a result will be done through the event loop running in the main thread.

No since the functions like e.g. setText() are not thread-safe.