简体   繁体   中英

How can I change Qt GUI widgets on a QMainWindow from a QThread which is a member?

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.

Using signal/slots. Trolltech introduces from 4.xa 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

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 QVariant s, 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>();

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

See the docs of Q_DECLARE_METATYPE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM