Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [Solved] Passing a custom QDeclarativeItem as a method Parameter?

    QML and Qt Quick
    3
    4
    1814
    Loading More Posts
    • 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.
    • G
      Gary_ last edited by

      I have a class MyItem that derrives from QDeclarativeItem.
      I have a 2nd class MyContainer that derrives from QDeclarativeItem.

      @
      MyItem {
      id: item1
      text: "Hello!"
      }

      MyContainer {
          id: container
      }
      
      Component.onCompleted: {
            console.log( container.returnItem(item1).text );
      }
      

      @

      Where

      @
      Q_INVOKABLE MyItem* returnItem(MyItem* item) { return item; }
      @

      Errors

      @
      TypeError: Result of expression 'container.returnItem(item1)' [undefined] is not an object.
      @

      Is it possible to pass a custom item into a method, or must I use a property?

      1 Reply Last reply Reply Quote 0
      • G
        Gary_ last edited by

        --- Solved ---

        @
        Q_INVOKABLE QObject* returnItem(QObject* item) { return item; }
        @

        And qobject_cast<MyItem*>(item) to make use in C++.

        1 Reply Last reply Reply Quote 0
        • M
          mbrasser last edited by

          Hi,

          This was a bug, but should be solved for Qt 4.7.1 and later (see "QTBUG-13047":http://bugreports.qt.nokia.com/browse/QTBUG-13047). Are you using 4.7.0, or still seeing problems in a later release?

          Regards,
          Michael

          1 Reply Last reply Reply Quote 0
          • M
            mdpoole last edited by

            I noticed something very similar (I would guess the same underlying cause) yesterday with Qt 4.7.2.

            In my case, I had a signal in my C++ code with signature void mySignal(CustomTypeDerivedFromQObject*). If I attached a Javascript function in QML to that signal, the function's parameter would print as QVariant<CustomTypeDerivedFromQObject*> but I could not access any properties of the object. By changing the signal's signature to void mySignal(QObject*), the QML code was able to see the object's properties as expected.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post