tags:

views:

969

answers:

2

I have my main form made QT Designer and inheriting from QMainWindow and the UI. I need to have other threads running, and I need those threads to change things on the main form, eg progress bars, LCDs.

How do I give the other thread access to the widgets on the main form?

Thanks for any help.

+3  A: 

Using signal/slots. Trolltech introduces from 4.x a threadsafe mechanism for signaling using for example the Qt::BlockingQueuedConnection parameter in connect() function.

For more details see: http://lists.trolltech.com/qt-interest/2007-03/thread00260-0.html

Flavius Suciu
+2  A: 

As Flavius Suciu has mentioned, you can use a cross-thread signal/slot connection. They can also carry arguments, however, if you don't pass just fundamental types or Qt types as signal parameters but, say, your own custom struct, you need to tell Qt about them this way:

namespace MyNamespace { // if any...
    struct MyClass { /* ... */ };
} // if any
Q_DECLARE_METATYPE( MyNamespace::MyClass )

This allows MyClass to be stuffed into QVariants, which is what Qt uses internally to ship copies of the signal arguments over thread boundaries.

You might also need to call

qRegisterMetatype<MyNamespace::MyClass>( "MyNamespace::MyClass" );

somewhere it's bound to be executed before any signal/slot cross-thread connection is attempted (e.g. in main(), or your QThread subclass constructor).

See the docs of Q_DECLARE_METATYPE