Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Learning
  3. Qt in Education
  4. [CLOSED]Event handling from C++ to QML
Forum Updated to NodeBB v4.3 + New Features

[CLOSED]Event handling from C++ to QML

Scheduled Pinned Locked Moved Qt in Education
4 Posts 2 Posters 3.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sam_dev
    wrote on last edited by
    #1

    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) {
    switch(eventId)
    {
    case IMAGE_UPDATE:
    {
    setImage(); //Sets a varaible defined via Q_PROPERTY
    . . .
    }
    break;
    }
    }@

    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

    1 Reply Last reply
    0
    • O Offline
      O Offline
      onek24
      wrote on last edited by
      #2

      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":http://qt-project.org/doc/qt-4.8/qtbinding.html#calling-functions
      "Receiving Signals":http://qt-project.org/doc/qt-4.8/qtbinding.html#receiving-signals
      "Supportet Datatypes":http://qt-project.org/doc/qt-4.8/qtbinding.html#supported-data-types

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sam_dev
        wrote on last edited by
        #3

        Thanks onek24...

        I understood your point.

        1 Reply Last reply
        0
        • O Offline
          O Offline
          onek24
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved