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

Accessing QQmlListProperty as QVariants from C++



  • I'm currently converting some of my code from QList<QObject*> properties to QQmlListProperty based properties due to the issues with memory leaks.

    So in my class I basically do this switch:

    class MyClass : public QObject
    {
        Q_OBJECT
    
        Q_PROPERTY(QList<QObject*> testedValue READ testedValue NOTIFY testedValueChanged)
        // this is now changed into
        Q_PROPERTY(QQmlListProperty<QObject> testedValue READ testedValue NOTIFY testedValueChanged)
        ...
    }
    

    Now I'm running into some issues with my Test-cases that I have implemented in C++. So far with QList<QObject*> I did something like the following to closely emulate the QML access of this member:

    QVariant propertyValue = testee.property("testedValue");
    QList<QObject*> testedList = propertyValue .value<QList<QObject*> >();
    ASSERT_EQ(expectedCount, testedList.count());
    

    In the new version of course I would switch to QQmlListProperty:

    QVariant propertyValue = testee.property("testedValue");
    QQmlListProperty<QObject> testedList = propertyValue .value<QQmlListProperty<QObject> >();
    ASSERT_EQ(expectedCount, testedList.count(&testedList));
    

    Now I get errors from the meta typing system at runtime and of course the tests fail. Actually they crash terribly since the .value<> conversion doesn't work at all, returns 0 and in the ASSERT_EQ line I'm accessing a NULL pointer. I see the following output in the tests:

    QMetaProperty::read: Unable to handle unregistered datatype 'QQmlListProperty<QObject>' for property 'MyClass::testedValue'

    What is the correct way to access this property from my C++ tests? Does it even make sense to try and access this value via the property() method?

    Alternatively I fear I have to access this value directly without ->property(). But then how do I access the items in the QQmlListProperty? Do I really have to use the public 'at', 'count', etc. members and check for nullptr?


Log in to reply