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

Can a QDeclarativeListProperty be accessed directly, instead of as a model?



  • I'm trying to use a QDeclarativeListProperty in order to manage a list of parameters, mostly for the purposes of displaying them in a ListView. However, I would also like to be able to directly access the parameters in QML from the QDeclarativeListProperty so that I can display/modify individual parameters on different screens.

    My class is called ParameterClass, for which I've created a QList:

    @
    class SystemData : public QObject
    {
    Q_OBJECT
    Q_PROPERTY(QDeclarativeListProperty<ParameterClass> parameters READ parameters NOTIFY parametersChanged)
    QDeclarativeListProperty<ParameterClass> parameters();

    ...
    
    QList<ParameterClass *> m_parameterList;
    

    }
    @
    I've also registered the ParameterClass class and set up an instance of my SystemData as a property, which I know is necessary.

    @
    m_context->setContextProperty("SystemData", m_pSystemData);
    qmlRegisterType<ParameterClass>();
    @

    Now, what I want to do within QML is something like this:

    @
    Rectangle {
    id: frame
    property variant parameter: SystemData.parameters[5]
    ...
    }
    @

    I'm just not getting it to work: I keep getting back [undefined]. Am I wasting my time, or am I missing something?



  • I'm not familiar with qt 4.x but I guess you can use "at function":http://harmattan-dev.nokia.com/docs/library/html/qt4/qdeclarativelistproperty.html#AtFunction-typedef directly inside qml as in qt 5.x.



  • [quote author="dasun" date="1382849565"]I'm not familiar with qt 4.x but I guess you can use "at function":http://harmattan-dev.nokia.com/docs/library/html/qt4/qdeclarativelistproperty.html#AtFunction-typedef directly inside qml as in qt 5.x.[/quote]

    That would work for a QList<QObject*>, but I'm working with a QDeclarativeListProperty.

    I now have this working. I think there was one error that I was facing that had to do with something unrelated to what I was trying to do here, but also I was informed "here":http://stackoverflow.com/questions/19503898/can-a-qdeclarativelistproperty-be-accessed-directly-instead-of-as-a-model that I needed to setup my context properties and registered types before loading my source URL. Once I rearranged my code to make that happen, everything did start to work. I don't know if that's a known issue or bug, and if the same goes for Qt 5.x.


Log in to reply