Solved Why are upper-cased properties not supported in QML?
-
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) -
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):
-
Hello @mwallnoefer,
The documentation currently refers to this in property attributes
-
@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.
-
Capital letter properties are reserved for so called attached properties (http://doc.qt.io/qt-5/qtqml-syntax-objectattributes.html).