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:
Invalid property name 'persons' (M16)
Of course, when I run it, I get
qrc:///main.qml:27 Cannot assign to non-existent property "persons"
../QQmlListProperty/main.cpp: 23
Which mistake do I make? Are there any official docs about QQmlListProperty that cover this?
Here's what I do (simplified compileable example):
main.qml
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"
}
]
}
}
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"
}
]
}
}
To copy to clipboard, switch view to plain text mode
Here's group.h:
#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
#ifndef GROUP_H
#define GROUP_H
#include "src/model/persons/person.h"
#include <QQmlListProperty>
class Person;
class Group: public QObject
{
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
To copy to clipboard, switch view to plain text mode
I have, of course, registered the classes in main.cpp:
#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);
component
->loadUrl
(QUrl(QStringLiteral
("qrc:///main.qml")));
if (!component->isReady() ) {
qWarning("%s", qPrintable(component->errorString()));
return -1;
}
QObject *topLevel
= component
->create
();
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;
}
#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[])
{
QApplication app(argc, 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);
QObject::connect(&engine, SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit()));
component->loadUrl(QUrl(QStringLiteral("qrc:///main.qml")));
if (!component->isReady() ) {
qWarning("%s", qPrintable(component->errorString()));
return -1;
}
QObject *topLevel = component->create();
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;
}
To copy to clipboard, switch view to plain text mode
Finally - for completeness - person.h
#ifndef PERSON_H
#define PERSON_H
#include <QString>
#include <QList>
#include <QObject>
#include "src/model/persons/group.h"
class Group;
{
Q_OBJECT
Q_PROPERTY(QString firstName READ getFirstName WRITE setFirstName NOTIFY firstNameChanged
) Q_PROPERTY(QString familyName READ getFamilyName WRITE setFamilyName NOTIFY familyNameChanged
)
signals:
void firstNameChanged();
void familyNameChanged();
public:
// getters
QString getFirstName
() {return m_firstName;
} QString getFamilyName
() {return m_familyName;
}
// setters
void setFirstName
(QString firstName
) {m_firstName
=firstName; emit firstNameChanged
();
} void setFamilyName
(QString familyName
) {m_familyName
=familyName; emit familyNameChanged
();
}
protected:
// members
};
#endif // PERSON_H
#ifndef PERSON_H
#define PERSON_H
#include <QString>
#include <QList>
#include <QObject>
#include "src/model/persons/group.h"
class Group;
class Person: public QObject
{
Q_OBJECT
Q_PROPERTY(QString firstName READ getFirstName WRITE setFirstName NOTIFY firstNameChanged)
Q_PROPERTY(QString familyName READ getFamilyName WRITE setFamilyName NOTIFY familyNameChanged)
signals:
void firstNameChanged();
void familyNameChanged();
public:
// getters
QString getFirstName() {return m_firstName;}
QString getFamilyName() {return m_familyName;}
// setters
void setFirstName(QString firstName) {m_firstName=firstName; emit firstNameChanged();}
void setFamilyName(QString familyName) {m_familyName=familyName; emit familyNameChanged();}
protected:
// members
QString m_firstName;
QString m_familyName;
};
#endif // PERSON_H
To copy to clipboard, switch view to plain text mode
Bookmarks