Hi,
I am trying to expose a QList<someObject*> to QML with the QQmlListProperty.
I will need it to be readable and writeable.
As the docs and explanations are at least unspecific, if not wrong, I've tried following this advice here:
https://bugreports.qt-project.org/br...comment-246411
(see comment from 09/Jun/14 8:07 PM)
When I try to use the property in QML, it is underlined in red:
Of course, when I run it, I getQuote:
Invalid property name 'persons' (M16)
Which mistake do I make? Are there any official docs about QQmlListProperty that cover this?Quote:
qrc:///main.qml:27 Cannot assign to non-existent property "persons"
../QQmlListProperty/main.cpp: 23
Here's what I do (simplified compileable example):
main.qml
Code:
import QtQuick 2.2 import QtQuick.Controls 1.1 import de.testing.qqmllistproperty.notworking 1.0 ApplicationWindow { id: applicationWindow1 visible: true width: 640 height: 480 Person { id: maxineMustermann firstName: "Maxine" familyName: "Mustermann" } Text { text: [maxineMustermann.familyName, maxineMustermann.firstName].join() //this works, so Person is registered correctly color: "#000000" x: 0 y: 0 } Group { persons: [ //<---------- This is line 27 mentioned in the error message Person { id: john firstName: "John" familyName: "Doe" }, Person { id: marcus firstName: "Marcus" familyName: "Miller" } ] } }
Here's group.h:
Code:
#ifndef GROUP_H #define GROUP_H #include "src/model/persons/person.h" #include <QQmlListProperty> class Person; { Q_OBJECT Q_PROPERTY(QQmlListProperty<Person> persons READ getPersons NOTIFY personsChanged) signals: void personsChanged(); public: QQmlListProperty <Person> getPersons() { return QQmlListProperty<Person>(this,0, &appendPerson,&personsCount,&personAt,&personsClear); } static void appendPerson(QQmlListProperty<Person> *list, Person *p) { Group *group = qobject_cast<Group*>(list->object); if(group && p) { group->m_persons.append(p); emit group->personsChanged(); } } static int personsCount(QQmlListProperty<Person>*list) { Group *group = qobject_cast<Group*>(list->object); if (group) return group->m_persons.count(); return 0; } static Person* personAt(QQmlListProperty<Person> *list, int i) { Group *group = qobject_cast<Group*>(list->object); if (group) return group->m_persons.at(i); return 0; } static void personsClear(QQmlListProperty<Person> *list) { Group *group = qobject_cast<Group*>(list->object); if (group) { group->m_persons.clear(); emit group->personsChanged(); } } protected: // members QList <Person*> m_persons; }; #endif // GROUP_H
I have, of course, registered the classes in main.cpp:
Code:
#include <QApplication> #include <QObject> #include <QQmlComponent> #include <QQmlEngine> #include <QQuickWindow> #include <QSurfaceFormat> #include "src/model/persons/group.h" #include "src/model/persons/person.h" int main(int argc, char *argv[]) { qmlRegisterType<Person>("de.testing.qqmllistproperty.notworking",1,0,"Person"); qmlRegisterType<Person>("de.testing.qqmllistproperty.notworking",1,0,"Group"); QQmlEngine engine; QQmlComponent *component = new QQmlComponent(&engine); if (!component->isReady() ) { qWarning("%s", qPrintable(component->errorString())); return -1; } QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel); QSurfaceFormat surfaceFormat = window->requestedFormat(); window->setFormat(surfaceFormat); window->show(); int appResult = 0; appResult = app.exec(); delete component; return appResult; }
Finally - for completeness - person.h
Code:
#ifndef PERSON_H #define PERSON_H #include <QString> #include <QList> #include <QObject> #include "src/model/persons/group.h" class Group; { Q_OBJECT signals: void firstNameChanged(); void familyNameChanged(); public: // getters // setters protected: // members QString m_firstName; QString m_familyName; }; #endif // PERSON_H