Solved Q_PROPERTY clarification
-
I want to define some
Q_PROPERTY
in 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
MEMBER
var, 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
name
field 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.
MEMBER
will generate the getter and the setter for you. Use like usual:qreal value = object.counterInTotal(); // Getter object.setCounterInTotal(0.12); // Setter
Also
counterInTotalChanged
should be a method that signals the property change:void counterInTotalChanged(qreal);
-
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); ^
-
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.
-
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.
MEMBER
should generate the read and write accessor functions for the property, so it's not only to expose it to QML. -
I can't post the code (like the above...) because when I click "submit" the website tells me the content was flagged as spam!
This happens even if I discard and create a new reply... -
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.
-
@Mark81 I had to remove the #ifdef to post it.
-
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 -
No problem.
I knew that thread but I was hoping Qt guys had fixed that after five years!Of course manually writing getter/setter functions work as expected. It's just a bit tedious if you have lot of properties!
-
Is it because the "MEMBER _counterInTotal" has an underscore ?
-
@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 ... :)