Problem with Q_PROPERTY - application crashes when I try to assign value.
-
Hi.
I added property to my class:Q_PROPERTY(QString guid READ guid WRITE setGuid NOTIFY guidChanged USER true) public: QString guid() const; void setGuid(const QString &value); signals: void guidChanged();and definitions:
QString MyClass::guid() const { return property("guid").toString(); } void MyClass::setGuid(const QString &value) { setProperty("guid", QVariant(value)); emit guidChanged(); }Application crashes on this line:
setProperty("guid", QVariant(value));And I don't understand what wrong?
-
I haven't worked with the property system that much so I am sorry if I am giving wrong information.
As far as I can tell the problem is that you use the property system to access the property. You cannot usesetProperty()andproperty()inside the functions that are accessed by the property system. You need a (private) member namedguidof typeQStringin your class. Then you implement the getter and setter functions to access and modify that string. This will allow other classes/objects to access that property through the property system.Note that it's also possible to give the class member that holds/represents the property a different name. In that case you would use the MEMBER attribute of the
Q_PROPERTYmacro to inform the property system which member this property is about.I hope that helps.
-
I haven't worked with the property system that much so I am sorry if I am giving wrong information.
As far as I can tell the problem is that you use the property system to access the property. You cannot usesetProperty()andproperty()inside the functions that are accessed by the property system. You need a (private) member namedguidof typeQStringin your class. Then you implement the getter and setter functions to access and modify that string. This will allow other classes/objects to access that property through the property system.Note that it's also possible to give the class member that holds/represents the property a different name. In that case you would use the MEMBER attribute of the
Q_PROPERTYmacro to inform the property system which member this property is about.I hope that helps.
@Joel-Bodenmann It helps, thank you.
-
Hi,
To add to @Joel-Bodenmann what you wrote is an infinite loop. setProperty will call your setter function which will call setProperty etc. hence infinite loop.
-
Hi,
To add to @Joel-Bodenmann what you wrote is an infinite loop. setProperty will call your setter function which will call setProperty etc. hence infinite loop.
@SGaist Thank you.