Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Link between Q_PROPERTY and member Variables.
Forum Updated to NodeBB v4.3 + New Features

Link between Q_PROPERTY and member Variables.

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 6.6k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Flavian
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • Yves MaurischatY Offline
      Yves MaurischatY Offline
      Yves Maurischat
      wrote on last edited by Yves Maurischat
      #2

      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.

      F ? 2 Replies Last reply
      0
      • Yves MaurischatY Yves Maurischat

        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.

        F Offline
        F Offline
        Flavian
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • Yves MaurischatY Offline
          Yves MaurischatY Offline
          Yves Maurischat
          wrote on last edited by Yves Maurischat
          #4

          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...

          1 Reply Last reply
          0
          • F Offline
            F Offline
            Flavian
            wrote on last edited by Flavian
            #5

            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.

            1 Reply Last reply
            0
            • Yves MaurischatY Offline
              Yves MaurischatY Offline
              Yves Maurischat
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • F Offline
                F Offline
                Flavian
                wrote on last edited by
                #7

                some other opinions?

                kshegunovK 1 Reply Last reply
                0
                • F Flavian

                  some other opinions?

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #8

                  @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.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  0
                  • Yves MaurischatY Yves Maurischat

                    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.

                    ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #9

                    @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.

                    1 Reply Last reply
                    1

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved