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.

    @
    void timeObserver()
    {
    if(InvokeRequired == true)
    {
    invoke()
    }
    else
    {
    setTime();
    }
    }
    @

    Any suggestions would be very helpful


  • Moderators

    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:



  • Thank you for the information.



  • 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)
    );
    }
    @


Log in to reply