Important: Please read the Qt Code of Conduct -

[CLOSED]Event handling from C++ to QML

  • Hello Everyone,

    Please provide your suggestion for below scenario:

    An external module sends an event to C++ thread to update an image in QML.

    I can handle this event in C++ as

    void myHandler(int eventId) {
    case IMAGE_UPDATE:
    setImage(); //Sets a varaible defined via Q_PROPERTY
    . . .

    How to send a signal to QML from C++ when an event is received to dynamically update the content of QML.

    Edit: fixed layout issues. Please at least surround code sections with @ tags; Andre

  • You could use QMetaObject::invokeMethod to invoke a QML method.
    For example: if you got QQuickView as your QMLProvider, you have to get the rootObject of it.
    @QQuickView *view = new QQuickView();
    QObject object = (QObject)view->rootObject();@

    To invoke the method you have to do:

    @QMetaObject::invokeMethod(object, "nameOfYourQmlFunction")@

    If you want to give parameters you'll have to do something like that:

    @QMetaObject::invokeMethod(object, "nameOfYourQmlFunction", Q_ARG(QVariant, "valueToSend"))@

    You can replace the String with booleans or integers, QML transforms them to it's required format. If you want to have some values returned you'll need a Q_RETURN_ARG instead of the Q_ARG.

    Function in the Documentation:
    @bool QMetaObject::invokeMethod(QObject * obj, const char * member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0 = QGenericArgument( 0 ), QGenericArgument val1 = QGenericArgument(), QGenericArgument val2 = QGenericArgument(), QGenericArgument val3 = QGenericArgument(), QGenericArgument val4 = QGenericArgument(), QGenericArgument val5 = QGenericArgument(), QGenericArgument val6 = QGenericArgument(), QGenericArgument val7 = QGenericArgument(), QGenericArgument val8 = QGenericArgument(), QGenericArgument val9 = QGenericArgument())[static]@

    For further information please read:
    "Calling Functions":
    "Receiving Signals":
    "Supportet Datatypes":

  • Thanks onek24...

    I understood your point.

  • Sorry, i didn't see that it's a thread from more than 1 month ago. I hope that i could still help you and/or someone else who didn't understand how to invoke QML methods.

Log in to reply