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. Q_PROPERTY clarification
Forum Updated to NodeBB v4.3 + New Features

Q_PROPERTY clarification

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 4.0k 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.
  • M Offline
    M Offline
    Mark81
    wrote on last edited by Mark81
    #1

    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?

    kshegunovK 1 Reply Last reply
    0
    • M Mark81

      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?

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

      @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);
      

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mark81
        wrote on last edited by
        #3

        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);
                   ^
        
        kshegunovK 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • M Mark81

            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);
                       ^
            
            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #5

            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.

            Read and abide by the Qt Code of Conduct

            M 1 Reply Last reply
            0
            • M Offline
              M Offline
              Mark81
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • kshegunovK kshegunov

                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.

                M Offline
                M Offline
                Mark81
                wrote on last edited by
                #7

                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.

                M 1 Reply Last reply
                0
                • M Mark81

                  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.

                  M Offline
                  M Offline
                  Mark81
                  wrote on last edited by
                  #8

                  @Mark81 I had to remove the #ifdef to post it.

                  kshegunovK 1 Reply Last reply
                  0
                  • M Mark81

                    @Mark81 I had to remove the #ifdef to post it.

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

                    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

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    1
                    • M Offline
                      M Offline
                      Mark81
                      wrote on last edited by
                      #10

                      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!

                      1 Reply Last reply
                      0
                      • mranger90M Offline
                        mranger90M Offline
                        mranger90
                        wrote on last edited by
                        #11

                        Is it because the "MEMBER _counterInTotal" has an underscore ?

                        kshegunovK 1 Reply Last reply
                        0
                        • mranger90M mranger90

                          Is it because the "MEMBER _counterInTotal" has an underscore ?

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

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

                          Read and abide by the Qt Code of Conduct

                          1 Reply Last reply
                          0

                          • Login

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