Results 1 to 2 of 2

Thread: QQmlListProperty Problem

  1. #1
    Join Date
    Jan 2012
    Location
    Dortmund, Germany
    Posts
    159
    Thanks
    69
    Thanked 10 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default QQmlListProperty Problem

    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
    Qt Code:
    1. import QtQuick 2.2
    2. import QtQuick.Controls 1.1
    3. import de.testing.qqmllistproperty.notworking 1.0
    4.  
    5. ApplicationWindow {
    6. id: applicationWindow1
    7. visible: true
    8. width: 640
    9. height: 480
    10.  
    11. Person {
    12. id: maxineMustermann
    13. firstName: "Maxine"
    14. familyName: "Mustermann"
    15. }
    16.  
    17. Text {
    18. text: [maxineMustermann.familyName, maxineMustermann.firstName].join()
    19. //this works, so Person is registered correctly
    20. color: "#000000"
    21. x: 0
    22. y: 0
    23. }
    24.  
    25.  
    26. Group {
    27. persons: [ //<---------- This is line 27 mentioned in the error message
    28. Person {
    29. id: john
    30. firstName: "John"
    31. familyName: "Doe"
    32. },
    33. Person {
    34. id: marcus
    35. firstName: "Marcus"
    36. familyName: "Miller"
    37. }
    38. ]
    39.  
    40. }
    41.  
    42. }
    To copy to clipboard, switch view to plain text mode 

    Here's group.h:
    Qt Code:
    1. #ifndef GROUP_H
    2. #define GROUP_H
    3.  
    4. #include "src/model/persons/person.h"
    5. #include <QQmlListProperty>
    6.  
    7. class Person;
    8.  
    9. class Group: public QObject
    10. {
    11. Q_OBJECT
    12. Q_PROPERTY(QQmlListProperty<Person> persons READ getPersons NOTIFY personsChanged)
    13.  
    14. signals:
    15. void personsChanged();
    16.  
    17. public:
    18. QQmlListProperty <Person> getPersons()
    19. {
    20. return QQmlListProperty<Person>(this,0, &appendPerson,&personsCount,&personAt,&personsClear);
    21. }
    22. static void appendPerson(QQmlListProperty<Person> *list, Person *p) {
    23. Group *group = qobject_cast<Group*>(list->object);
    24. if(group && p) {
    25. group->m_persons.append(p);
    26. emit group->personsChanged();
    27. }
    28. }
    29.  
    30. static int personsCount(QQmlListProperty<Person>*list)
    31. {
    32. Group *group = qobject_cast<Group*>(list->object);
    33. if (group)
    34. return group->m_persons.count();
    35. return 0;
    36. }
    37.  
    38. static Person* personAt(QQmlListProperty<Person> *list, int i)
    39. {
    40. Group *group = qobject_cast<Group*>(list->object);
    41. if (group)
    42. return group->m_persons.at(i);
    43. return 0;
    44.  
    45. }
    46.  
    47. static void personsClear(QQmlListProperty<Person> *list)
    48. {
    49. Group *group = qobject_cast<Group*>(list->object);
    50. if (group) {
    51. group->m_persons.clear();
    52. emit group->personsChanged();
    53. }
    54. }
    55.  
    56.  
    57. protected:
    58. // members
    59. QList <Person*> m_persons;
    60.  
    61. };
    62.  
    63. #endif // GROUP_H
    To copy to clipboard, switch view to plain text mode 

    I have, of course, registered the classes in main.cpp:
    Qt Code:
    1. #include <QApplication>
    2. #include <QObject>
    3. #include <QQmlComponent>
    4. #include <QQmlEngine>
    5. #include <QQuickWindow>
    6. #include <QSurfaceFormat>
    7. #include "src/model/persons/group.h"
    8. #include "src/model/persons/person.h"
    9.  
    10. int main(int argc, char *argv[])
    11. {
    12. QApplication app(argc, argv);
    13.  
    14. qmlRegisterType<Person>("de.testing.qqmllistproperty.notworking",1,0,"Person");
    15. qmlRegisterType<Person>("de.testing.qqmllistproperty.notworking",1,0,"Group");
    16.  
    17.  
    18. QQmlEngine engine;
    19. QQmlComponent *component = new QQmlComponent(&engine);
    20. QObject::connect(&engine, SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit()));
    21. component->loadUrl(QUrl(QStringLiteral("qrc:///main.qml")));
    22. if (!component->isReady() ) {
    23. qWarning("%s", qPrintable(component->errorString()));
    24. return -1;
    25. }
    26. QObject *topLevel = component->create();
    27. QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
    28. QSurfaceFormat surfaceFormat = window->requestedFormat();
    29. window->setFormat(surfaceFormat);
    30. window->show();
    31.  
    32. int appResult = 0;
    33. appResult = app.exec();
    34.  
    35. delete component;
    36. return appResult;
    37. }
    To copy to clipboard, switch view to plain text mode 

    Finally - for completeness - person.h
    Qt Code:
    1. #ifndef PERSON_H
    2. #define PERSON_H
    3.  
    4. #include <QString>
    5. #include <QList>
    6. #include <QObject>
    7. #include "src/model/persons/group.h"
    8.  
    9.  
    10. class Group;
    11.  
    12.  
    13.  
    14. class Person: public QObject
    15. {
    16. Q_OBJECT
    17. Q_PROPERTY(QString firstName READ getFirstName WRITE setFirstName NOTIFY firstNameChanged)
    18. Q_PROPERTY(QString familyName READ getFamilyName WRITE setFamilyName NOTIFY familyNameChanged)
    19.  
    20. signals:
    21. void firstNameChanged();
    22. void familyNameChanged();
    23.  
    24. public:
    25. // getters
    26. QString getFirstName() {return m_firstName;}
    27. QString getFamilyName() {return m_familyName;}
    28.  
    29. // setters
    30. void setFirstName(QString firstName) {m_firstName=firstName; emit firstNameChanged();}
    31. void setFamilyName(QString familyName) {m_familyName=familyName; emit familyNameChanged();}
    32.  
    33. protected:
    34. // members
    35. QString m_firstName;
    36. QString m_familyName;
    37. };
    38.  
    39. #endif // PERSON_H
    To copy to clipboard, switch view to plain text mode 
    Last edited by sedi; 5th October 2014 at 02:28.

  2. #2
    Join Date
    Jan 2012
    Location
    Dortmund, Germany
    Posts
    159
    Thanks
    69
    Thanked 10 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Lightbulb Re: QQmlListProperty Problem

    Ouch. Never mind, sorry.

    I've just found my somewhat silly mistake in main.cpp

    Instead of :
    Qt Code:
    1. qmlRegisterType<Person>("de.testing.qqmllistproperty.notworking",1,0,"Person");
    2. qmlRegisterType<Person>("de.testing.qqmllistproperty.notworking",1,0,"Group");
    To copy to clipboard, switch view to plain text mode 

    It must - of course - be:
    Qt Code:
    1. qmlRegisterType<Person>("de.testing.qqmllistproperty.notworking",1,0,"Person");
    2. qmlRegisterType<Group>("de.testing.qqmllistproperty.notworking",1,0,"Group");
    To copy to clipboard, switch view to plain text mode 

    The moment you're doing things right, they actually start working....

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.