Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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?



  • --- Solved ---

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

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



  • 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



  • 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.


Log in to reply