PDA

View Full Version : Reading widgets from thread



sirlore
24th June 2009, 21:53
Hi

I'm just wondering if doing something like widget->value() to read value is possible in thread. I'm just asking because doing it with signal/slots is a bit cumbersome.

wysota
24th June 2009, 21:59
No, it's not possible. You can operate on widget from the main thread only. You have to use events or signals to transfer widget related data across threads.

sirlore
24th June 2009, 22:33
No, it's not possible. You can operate on widget from the main thread only. You have to use events or signals to transfer widget related data across threads.

thanks for fast reply.

I must say I was reading widgets in threads by just using pointers, as mentioned before, but any modification such as widget->setValue was made in main thread using signals slots. I haven't noticed any error or crush of my apps so far using this "method" of accessing widgets. There is no word about reading widgets "this way" in qt documentation, nor strict forbidding this.

wysota
24th June 2009, 22:58
There is no word about reading widgets "this way" in qt documentation, nor strict forbidding this.

To quote the docs:

Although QObject is reentrant, the GUI classes, notably QWidget and all its subclasses, are not reentrant. They can only be used from the main thread.

Reading is "quite safe" most of the time in general. In most cases the worst thay may happen is that you get a wrong value. Notice the "in most cases" part... Of course you might be satisfied with your application working correctly "in most cases".