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. Custom QQuickItem -- accessing QML functions from c++
Forum Updated to NodeBB v4.3 + New Features

Custom QQuickItem -- accessing QML functions from c++

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

    Accessing C++ functions in a subclassed QQuickItem from the QML side is easy via Q_INVOKABLE but what if I want to go in reverse and call QML functions from c++? I've seen several examples that require finding the QML object first so you know what object to call on, but that would only seem necessary if a different c++ object is calling functions in the QML -- but since the C++ object of a QQuickItem is the same C++ object as the QML object, what is the easy way to get Q_INVOKABLE style communication going, but in the other direction so my C++ can access QML functions and properties?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      See QObject's "property()":http://qt-project.org/doc/qt-5/qobject.html#property and "setProperty()":http://qt-project.org/doc/qt-5/qobject.html#setProperty, as well as QMetaObject's "invokeMethod()":http://qt-project.org/doc/qt-5/qmetaobject.html#invokeMethod.

      (Z(:^

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qttester5
        wrote on last edited by
        #3

        I've used invokeMethod before but it requires you pass in the object you are calling the method on. This makes sense if you are invoking a QML method on an object that is separate from the calling C++ code. But what if the QML method you are invoking is in the QML part of the C++ QQuickItem? What do you pass to invokeMethod() to indicate you are using the "this" object? I can't find any information in the docs about this seemingly straightforward task.

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          You have pretty much answered yourself: pass this as the object :)

          (Z(:^

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qttester5
            wrote on last edited by
            #5

            Well of course I tried that. It does not work. invokeMethod has no overload for the custom type that this represents and that is what the compiler reports.

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              You must be doing something wrong. QQuickItem is derived from QObject: it has to work, and actually does work, I use it all over the place in one of my projects.

              If it really fails, try casting the pointer (qobject_cast). But that should really not be necessary.

              (Z(:^

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                qttester5
                wrote on last edited by
                #7

                I got it to work by simply doing this:

                QObject*t=this;

                This essentially casts it and I can then pass t as the the object. Wise? It is working now and I can call the QML function in this way.

                There appears to be no other way to directly pass a custom class to invokeMethod. I agree that it shouldn't make a difference since my this class derives from QQuickItem which derives from QObject. I wonder what is going on?

                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