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. Accessing QQmlListProperty as QVariants from C++
Forum Updated to NodeBB v4.3 + New Features

Accessing QQmlListProperty as QVariants from C++

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

    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?

    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