Link between Q_PROPERTY and member Variables.



  • Hello, I'm a little newbie at this, but i can't figure it out, how the Q_property is filling my private members in a class.

    Consider this example:
    http://doc.qt.io/qt-4.8/qt-declarative-cppextensions-referenceexamples-adding-example.html

    Is there a typing convension: the member variables should have "m_" attached, is this the convention? Or i am getting this all wrong?



  • In Q_PROPERTY you define the name of the qml property as well as the getter and setter methods. To fill your private member you need to implement the getter and setter methods. You also should have a change signal defined after NOTIFY in there, to notify QML about property changes.

    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
    ... 
    public:
        QString name() const { return m_name; }
        void setName(const QString &name) { m_name = name; emit nameChanged() } 
    signals:
        void nameChanged();
    

    Q_PROPERTY is solely for defining a property and making it known to the QML context, it does nothing in regards to your internal members.



  • @Yves-Maurischat Are you sure?

    Here: http://doc.qt.io/qt-4.8/qt-declarative-cppextensions-referenceexamples-grouped-example.html

    In person.h:

    class Person : public QObject
    {
    .....
    Q_PROPERTY(ShoeDescription *shoe READ shoe)
    ....
    QString m_name;
    ShoeDescription m_shoe;
    };
    It doesn't have a setter method, but it works, m_shoe gets filled.



  • Never seen that. I guess, if that works, it might be valid as well. But the example only defines READ, that means you can't assign a value to the shoe property in QML. You need to have WRITE defined to be able to assign values to the property in QML.
    Did you try the example and did it work? Ther are quite a few examples in the documentation that are incomplete or plain wrong...



  • It worked, I was studying it and I was pretty confused.

    And I wondered why did it work?
    Then I checked: http://doc.qt.io/qt-5/properties.html

    I was thinking that it might get the variable member name from within the get function and do the mapping automatically, but that's why I'm asking, because I have doubts.



  • In the documention you posted, they used MEMBER, not READ to access a member variable directly (that's why I think the other example is not correct):

    Q_PROPERTY(QString text MEMBER m_text NOTIFY textChanged)
    

    There it also says:

    A MEMBER variable association is required if no READ accessor function is specified. This makes the given member variable readable and writable without the need of creating READ and WRITE accessor functions. It's still possible to use READ or WRITE accessor functions in addition to MEMBER variable association (but not both), if you need to control the variable access.
    

    That means if you need to read AND write to a member variable you can't use MEMBER and must use READ and WRITE and associated getter/setter methods.



  • some other opinions?


  • Qt Champions 2016

    @Flavian
    Nope, @Yves-Maurischat is correct. The reason you don't see a getter/setter is because when you use MEMBER the moc will generate them for you based on READ/WRITE. If you don't use MEMBER, then it's your job to provide them and Q_PROPERTY only registers the property with the meta-object system.

    Kind regards.



  • @Yves-Maurischat said:

    Q_PROPERTY is solely for defining a property and making it known to the QML context

    I'm sure everyone here already knows that but just for the record: Qt's property system is older than QML and serves more purposes than just that.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.