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. [Solved] Passing a custom QDeclarativeItem as a method Parameter?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 2.0k Views
  • 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 Offline
    G Offline
    Gary_
    wrote on last edited by
    #1

    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
    0
    • G Offline
      G Offline
      Gary_
      wrote on last edited by
      #2

      --- Solved ---

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

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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mbrasser
        wrote on last edited by
        #3

        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
        0
        • M Offline
          M Offline
          mdpoole
          wrote on last edited by
          #4

          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
          0

          • Login

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