Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Call QML function directly from Qt(C++) in 4.8
Forum Updated to NodeBB v4.3 + New Features

Call QML function directly from Qt(C++) in 4.8

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 2 Posters 1.2k 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.
  • A Offline
    A Offline
    athavan
    wrote on last edited by
    #1

    I want to call the QML function directly from c++?

    1 Reply Last reply
    0
    • shavS Offline
      shavS Offline
      shav
      wrote on last edited by
      #2

      Hi,

      Did you try "this":http://qt-project.org/doc/qt-5/qtqml-cppintegration-interactqmlfromcpp.html#invoking-qml-methods ?

      Mac OS and iOS Developer

      1 Reply Last reply
      0
      • A Offline
        A Offline
        athavan
        wrote on last edited by
        #3

        Hi shav, Actually I am using Qt4.8.... so QQmlEngine,QQmlComponent are not available.... please give solution for 4.8

        1 Reply Last reply
        0
        • shavS Offline
          shavS Offline
          shav
          wrote on last edited by
          #4

          Oh... I'm sorry I've use Qt5.3 and post sample from Qt5.3 doc. I've found this code:
          @
          // main.cpp
          QDeclarativeEngine engine;
          QDeclarativeComponent component(&engine, "MyItem.qml");
          QObject *object = component.create();

          QVariant returnedValue;
          QVariant msg = "Hello from C++";
          QMetaObject::invokeMethod(object, "myQmlFunction",
          Q_RETURN_ARG(QVariant, returnedValue),
          Q_ARG(QVariant, msg));

          qDebug() << "QML function returned:" << returnedValue.toString();
          delete object;
          @

          Sorry I can't check it now because I don't have Qt 4.8 on my PC. But this code from official Qt4.8 documentation. You can use this "link":http://qt-project.org/doc/qt-4.8/qtbinding.html#calling-functions for more information.

          Mac OS and iOS Developer

          1 Reply Last reply
          0
          • A Offline
            A Offline
            athavan
            wrote on last edited by
            #5

            Hi shav , It is working fine, but I can't able to access the components (like Image,rectangle) in that QML functions... while try to access below error is through .....

            QObject::installEventFilter(): Cannot filter events for objects in a different thread.
            QPixmap: It is not safe to use pixmaps outside the GUI thread

            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