Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
[SOLVED] System::Windows::Forms::Control::InvokeRequired Qt equivalent
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.
if(InvokeRequired == true)
Any suggestions would be very helpful
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.
- "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
Thank you for the information.
I believe I have found what i was looking for.
In case someone else will find this useful:
define simTime as a slot in class definition.
void timeObserver(const int64_t x)
Q_ARG(const int64_t, x)