Q_PROPERTY clarification
-
I want to define some
Q_PROPERTYin order to access them from QML. I'm reading the docs here but I'm not sure about the syntax.As far as I understand if I don't need special accessor functions I can just define the
MEMBERvar, example:public: Q_PROPERTY(qreal counterInTotal MEMBER _counterInTotal NOTIFY counterInTotalChanged) private: qreal _counterInTotal; signals: void counterInTotalChanged;I would expect I can now access read/write my property as
counterInTotal = 0;even in C++. But it doesn't work, the variable is not found.What is the purpose of the
namefield here? How to use properties in the simplest mode (getter and setter functions don't do anything special) with the notification for use in QML? -
I want to define some
Q_PROPERTYin order to access them from QML. I'm reading the docs here but I'm not sure about the syntax.As far as I understand if I don't need special accessor functions I can just define the
MEMBERvar, example:public: Q_PROPERTY(qreal counterInTotal MEMBER _counterInTotal NOTIFY counterInTotalChanged) private: qreal _counterInTotal; signals: void counterInTotalChanged;I would expect I can now access read/write my property as
counterInTotal = 0;even in C++. But it doesn't work, the variable is not found.What is the purpose of the
namefield here? How to use properties in the simplest mode (getter and setter functions don't do anything special) with the notification for use in QML?@Mark81 said in Q_PROPERY clarification:
What is the purpose of the name field here?
Name of the property.
MEMBERwill generate the getter and the setter for you. Use like usual:qreal value = object.counterInTotal(); // Getter object.setCounterInTotal(0.12); // SetterAlso
counterInTotalChangedshould be a method that signals the property change:void counterInTotalChanged(qreal); -
Hi,
AFAIK, MEMBER triggers moc to generate additional code that enables the access to the property through the meta object system i.e. what's is used to interface with QML. If you want to modify the properties through C++, then you can use the setProperty method.
-
This does not compile. If I try:
this->setCounterInTotal(0)the build fails with:
error: ‘class MyClass’ has no member named ‘setCounterInTotal’ this->setCounterInTotal(0); ^Please show current (exact code) and also say if you reran qmake and rebuilt.
@SGaist said in Q_PROPERTY clarification:
If you want to modify the properties through C++, then you can use the setProperty method.
MEMBERshould generate the read and write accessor functions for the property, so it's not only to expose it to QML. -
Please show current (exact code) and also say if you reran qmake and rebuilt.
@SGaist said in Q_PROPERTY clarification:
If you want to modify the properties through C++, then you can use the setProperty method.
MEMBERshould generate the read and write accessor functions for the property, so it's not only to expose it to QML.Here my complete test code:
myclass.h
class MyClass : public QObject { Q_OBJECT public: explicit MyClass(QObject *parent = nullptr); Q_PROPERTY(qreal counterInTotal MEMBER _counterInTotal NOTIFY counterInTotalChanged) private: qreal _counterInTotal; signals: void counterInTotalChanged(qreal); };myclass.c
#include "myclass.h"MyClass::MyClass(QObject *parent) : QObject(parent) { this->setCounterInTotal(0); }Run qmake. Run build:
myclass.cpp:5: error: ‘class MyClass’ has no member named ‘setCounterInTotal’ this->setCounterInTotal(0); ^I'm using Qt5.9.2 under Debian cross-compiled for ARM.
-
Here my complete test code:
myclass.h
class MyClass : public QObject { Q_OBJECT public: explicit MyClass(QObject *parent = nullptr); Q_PROPERTY(qreal counterInTotal MEMBER _counterInTotal NOTIFY counterInTotalChanged) private: qreal _counterInTotal; signals: void counterInTotalChanged(qreal); };myclass.c
#include "myclass.h"MyClass::MyClass(QObject *parent) : QObject(parent) { this->setCounterInTotal(0); }Run qmake. Run build:
myclass.cpp:5: error: ‘class MyClass’ has no member named ‘setCounterInTotal’ this->setCounterInTotal(0); ^I'm using Qt5.9.2 under Debian cross-compiled for ARM.
-
Indeed, it seems @SGaist is correct and I'm wrong. I could've sworn it used to generate the accessor functions. Anyway, in light of that, I have to say you need to write them manually ...
Apparently I have lived in a parallel universe:
See here https://forum.qt.io/topic/29072/solved-use-of-q_property-s-member-keyword-outside-of-qml -
@mranger90 said in Q_PROPERTY clarification:
Is it because the "MEMBER _counterInTotal" has an underscore ?
Nope, not really. It just never generated the functions, it seems to have been some kind of urban legend ... :)