[SOLVED] System::Windows::Forms::Control::InvokeRequired Qt equivalent
-
wrote on 12 Mar 2014, 18:55 last edited by
I'm trying to translate a windows form program to Qt forms. I'm current building a custom widget inheriting QLineEdit.
Is there an equivalent to System::Windows::Forms::Control::InvokeRequired in Qt? I need to check if my Class's observer function is calling another method from a different thread.
I have a kernel running on "mainThread"
MyClass probably running on a "workerThread"
Then i add a time observer "kernel->addObserver(&MyClass::timeObserver)"I need to check inside timeObserver() if it was called from a different thread.
@
void timeObserver()
{
if(InvokeRequired == true)
{
invoke()
}
else
{
setTime();
}
}
@Any suggestions would be very helpful
-
Hi,
Qt does not have anything like Control.InvokeRequired, because only one thread is allowed to create controls (GUI objects) and call their member functions. This thread is called the "GUI thread". It is defined as the thread which instantiates your QApplication/QGuiApplication object.
For safe interaction between threads, use Qt's signals+slots mechanism. Qt will automatically check if the caller is in the same thread or not.
Further reading:
- "QObject|Thread Affinity":http://qt-project.org/doc/qt-5/QObject.html#thread-affinity
- "Synchronizing Threads|High-Level Event Queues":http://qt-project.org/doc/qt-5/threads-synchronizing.html#high-level-event-queues
- "Signals & Slots":http://qt-project.org/doc/qt-5/signalsandslots.html
-
wrote on 13 Mar 2014, 02:13 last edited by
Thank you for the information.
-
wrote on 17 Mar 2014, 05:45 last edited by
I believe I have found what i was looking for.
In case someone else will find this useful:
QMetaObject::invokeMethod()define simTime as a slot in class definition.
@
void timeObserver(const int64_t x)
{
QMetaObject::invokeMetho(this,
"setTime",
Qt::AutoConnection,
Q_ARG(const int64_t, x)
);
}
@
1/4