Why are upper-cased properties not supported in QML?
-
wrote on 25 Oct 2016, 10:31 last edited by mwallnoefer
What is the reason that Qt properties need to begin with a lower-cased letter in order to be exposed to QML?
Example: this works
C++
class Model : public QAbstractListModel { ... Q_PROPERTY(int name READ name WRITE setName NOTIFY nameChanged) Q_PROPERTY(int age READ age WRITE setAge NOTIFY ageChanged) ... };
QML:
Person { name: "Otto" age: 30 ... }
This not (odd error message in the QML code at runtime):
class Model : public QAbstractListModel { ... Q_PROPERTY(int Name READ name WRITE setName NOTIFY nameChanged) Q_PROPERTY(int Age READ age WRITE setAge NOTIFY ageChanged) ... };
QML:
Person { Name: "Otto" Age: 30 ... }
-
What is the reason that Qt properties need to begin with a lower-cased letter in order to be exposed to QML?
Example: this works
C++
class Model : public QAbstractListModel { ... Q_PROPERTY(int name READ name WRITE setName NOTIFY nameChanged) Q_PROPERTY(int age READ age WRITE setAge NOTIFY ageChanged) ... };
QML:
Person { name: "Otto" age: 30 ... }
This not (odd error message in the QML code at runtime):
class Model : public QAbstractListModel { ... Q_PROPERTY(int Name READ name WRITE setName NOTIFY nameChanged) Q_PROPERTY(int Age READ age WRITE setAge NOTIFY ageChanged) ... };
QML:
Person { Name: "Otto" Age: 30 ... }
@mwallnoefer
because there are default created changed-slots for each property following the pattern on<CamelCasePropertyName>Changed() (if NOTIFY was specified) -
wrote on 27 Oct 2016, 08:58 last edited by
Okay, but it seems that also the following piece does not work or am I missing something? Also when omitting the NOTIFY clause I get always the same "Invalid attached object assignment" error.
class Model : public QAbstractListModel { ... Q_PROPERTY(int Name READ Name WRITE setName NOTIFY NameChanged) Q_PROPERTY(int Age READ Age WRITE setAge NOTIFY AgeChanged) ... };
For me this seems more like a bug, since in C++ code upper cased properties are perfectly valid. Alternatively, the docs could be updated to contain an explicit reference to this restriction of QML (for instance in the section about Q_PROPERTY):
-
Okay, but it seems that also the following piece does not work or am I missing something? Also when omitting the NOTIFY clause I get always the same "Invalid attached object assignment" error.
class Model : public QAbstractListModel { ... Q_PROPERTY(int Name READ Name WRITE setName NOTIFY NameChanged) Q_PROPERTY(int Age READ Age WRITE setAge NOTIFY AgeChanged) ... };
For me this seems more like a bug, since in C++ code upper cased properties are perfectly valid. Alternatively, the docs could be updated to contain an explicit reference to this restriction of QML (for instance in the section about Q_PROPERTY):
wrote on 27 Oct 2016, 09:15 last edited byHello @mwallnoefer,
The documentation currently refers to this in property attributes
-
Hello @mwallnoefer,
The documentation currently refers to this in property attributes
wrote on 27 Oct 2016, 09:33 last edited by@Julien-B Okay, but then it should go also in the Q_PROPERTY section.
But the interesting thing is, that person.Name or person.Age does work though. Only the class assignment syntax with the colon does not.
-
wrote on 27 Oct 2016, 20:26 last edited by
Capital letter properties are reserved for so called attached properties (http://doc.qt.io/qt-5/qtqml-syntax-objectattributes.html).
6/6