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 and reference data type
Qt 6.11 is out! See what's new in the release blog

Q_Property and reference data type

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.4k Views
  • 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.
  • MasterQM Offline
    MasterQM Offline
    MasterQ
    wrote on last edited by MasterQ
    #1

    Hi,

    I try to start with Qt's property mechanism.

    The following is a first attemp

    #ifndef DBCONTEXT_H
    #define DBCONTEXT_H
    
    #include "dbset.h"
    #include "dokumententity.h"
    #include "qsqldatabase.h"
    #include "quoteentity.h"
    
    #include <QObject>
    
    class DBContext : QObject {
      Q_OBJECT
    public:
      DBContext();
      ~DBContext();
      Q_PROPERTY(DBSet<DocumentEntity> Documents READ Documents CONSTANT)
    
      DBSet<DocumentEntity> Documents() const;
    
    private:
      DBSet<DocumentEntity> m_Documents;
    };
    #endif // DBCONTEXT_H
    
    

    and

    DBSet<DocumentEntity> DBContext::Documents() const { return m_Documents; }
    

    this seems to work, but...

    ... should it be a better way to give reference type to avoid unnecessary copying?

    DBSet<DocumentEntity> &DBContext::Documents() const { return &m_Documents; }
    

    And this fails with

    dbcontext.cpp:29:62: Non-const lvalue reference to type 'DBSet<DocumentEntity>' cannot bind to a temporary of type 'const DBSet<DocumentEntity> *'
    

    I am confused since the lvalue is not taken as const as in the declaration.

    C++ const behaviour is still some miracle for me.

    Any hints, how to deal with it? And how should Q_PROPERTY look like to deal with reference types?

    greets

    Joachim

    JoeCFDJ 1 Reply Last reply
    0
    • MasterQM MasterQ

      Hi,

      I try to start with Qt's property mechanism.

      The following is a first attemp

      #ifndef DBCONTEXT_H
      #define DBCONTEXT_H
      
      #include "dbset.h"
      #include "dokumententity.h"
      #include "qsqldatabase.h"
      #include "quoteentity.h"
      
      #include <QObject>
      
      class DBContext : QObject {
        Q_OBJECT
      public:
        DBContext();
        ~DBContext();
        Q_PROPERTY(DBSet<DocumentEntity> Documents READ Documents CONSTANT)
      
        DBSet<DocumentEntity> Documents() const;
      
      private:
        DBSet<DocumentEntity> m_Documents;
      };
      #endif // DBCONTEXT_H
      
      

      and

      DBSet<DocumentEntity> DBContext::Documents() const { return m_Documents; }
      

      this seems to work, but...

      ... should it be a better way to give reference type to avoid unnecessary copying?

      DBSet<DocumentEntity> &DBContext::Documents() const { return &m_Documents; }
      

      And this fails with

      dbcontext.cpp:29:62: Non-const lvalue reference to type 'DBSet<DocumentEntity>' cannot bind to a temporary of type 'const DBSet<DocumentEntity> *'
      

      I am confused since the lvalue is not taken as const as in the declaration.

      C++ const behaviour is still some miracle for me.

      Any hints, how to deal with it? And how should Q_PROPERTY look like to deal with reference types?

      greets

      Joachim

      JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by JoeCFD
      #2

      @MasterQ C++ does not allow overloading a function with different return types.
      in your case, the two funcs are same, but return types are different.

      MasterQM 1 Reply Last reply
      0
      • JoeCFDJ JoeCFD

        @MasterQ C++ does not allow overloading a function with different return types.
        in your case, the two funcs are same, but return types are different.

        MasterQM Offline
        MasterQM Offline
        MasterQ
        wrote on last edited by MasterQ
        #3

        @JoeCFD

        sry, maybe my post was misleading

        class DBContext : QObject {
          Q_OBJECT
        public:
          DBContext();
          ~DBContext();
        
          Q_PROPERTY(DBSet<DocumentEntity> Documents READ Documents CONSTANT)
          Q_PROPERTY(DBSet<BillEntity> Bills READ Bills CONSTANT)
        
          DBSet<DocumentEntity> Documents() const { return m_Documents; }
          
          DBSet<BillEntity> &Bills() const { return &m_Bills; };
        
          private:
          const DBSet<DocumentEntity> m_Documents;
          const DBSet<BillEntity> m_Bills;
        
        };
        

        The first version of the getter (Documents) returns a full copy of the DBSet. This is, to my opinion, not a good solution. The second getter (Bills) returns a reference. While syntax of version 1 is OK, the error given above is thrown for version 2 with reference type.

        Question:
        Why is the lvalue not recognized as const in version2? How to do it right?

        Another question: How to write the line with Q_PROPERTY to achieve a getter with returning reference type?

        JoeCFDJ jsulmJ 2 Replies Last reply
        0
        • MasterQM MasterQ

          @JoeCFD

          sry, maybe my post was misleading

          class DBContext : QObject {
            Q_OBJECT
          public:
            DBContext();
            ~DBContext();
          
            Q_PROPERTY(DBSet<DocumentEntity> Documents READ Documents CONSTANT)
            Q_PROPERTY(DBSet<BillEntity> Bills READ Bills CONSTANT)
          
            DBSet<DocumentEntity> Documents() const { return m_Documents; }
            
            DBSet<BillEntity> &Bills() const { return &m_Bills; };
          
            private:
            const DBSet<DocumentEntity> m_Documents;
            const DBSet<BillEntity> m_Bills;
          
          };
          

          The first version of the getter (Documents) returns a full copy of the DBSet. This is, to my opinion, not a good solution. The second getter (Bills) returns a reference. While syntax of version 1 is OK, the error given above is thrown for version 2 with reference type.

          Question:
          Why is the lvalue not recognized as const in version2? How to do it right?

          Another question: How to write the line with Q_PROPERTY to achieve a getter with returning reference type?

          JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by
          #4

          @MasterQ said in Q_Property and reference data type:

          DBSet<BillEntity> &Bills() const { return &m_Bills; };

          const DBSet<BillEntity> &Bills() const { return &m_Bills; };
          
          1 Reply Last reply
          0
          • MasterQM MasterQ

            @JoeCFD

            sry, maybe my post was misleading

            class DBContext : QObject {
              Q_OBJECT
            public:
              DBContext();
              ~DBContext();
            
              Q_PROPERTY(DBSet<DocumentEntity> Documents READ Documents CONSTANT)
              Q_PROPERTY(DBSet<BillEntity> Bills READ Bills CONSTANT)
            
              DBSet<DocumentEntity> Documents() const { return m_Documents; }
              
              DBSet<BillEntity> &Bills() const { return &m_Bills; };
            
              private:
              const DBSet<DocumentEntity> m_Documents;
              const DBSet<BillEntity> m_Bills;
            
            };
            

            The first version of the getter (Documents) returns a full copy of the DBSet. This is, to my opinion, not a good solution. The second getter (Bills) returns a reference. While syntax of version 1 is OK, the error given above is thrown for version 2 with reference type.

            Question:
            Why is the lvalue not recognized as const in version2? How to do it right?

            Another question: How to write the line with Q_PROPERTY to achieve a getter with returning reference type?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @MasterQ said in Q_Property and reference data type:

            DBSet<BillEntity> &Bills() const { return &m_Bills; };

            This is wrong: you're trying to return a pointer, not reference!
            Should be:

            const DBSet<BillEntity> &Bills() const { return m_Bills; };
            

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            MasterQM 1 Reply Last reply
            0
            • jsulmJ jsulm

              @MasterQ said in Q_Property and reference data type:

              DBSet<BillEntity> &Bills() const { return &m_Bills; };

              This is wrong: you're trying to return a pointer, not reference!
              Should be:

              const DBSet<BillEntity> &Bills() const { return m_Bills; };
              
              MasterQM Offline
              MasterQM Offline
              MasterQ
              wrote on last edited by MasterQ
              #6

              @jsulm

              dbcontext.cpp:29:62: Binding reference of type 'DBSet<...>' to value of type 'const DBSet<...>' drops 'const' qualifier
              

              I had tried this also before. But the main issue remains. Why is "const" dropped?

              This is not simply a warning, it is an error!

              --EDIT--

              OK, I got you poltergeist!

              I just deleted all and rewrote the whole thing new. Now it is working and no errors about dropping const appears anymore.

              Just wasted a whole day by struggling with this bullsh*t

              jsulmJ 1 Reply Last reply
              0
              • MasterQM MasterQ

                @jsulm

                dbcontext.cpp:29:62: Binding reference of type 'DBSet<...>' to value of type 'const DBSet<...>' drops 'const' qualifier
                

                I had tried this also before. But the main issue remains. Why is "const" dropped?

                This is not simply a warning, it is an error!

                --EDIT--

                OK, I got you poltergeist!

                I just deleted all and rewrote the whole thing new. Now it is working and no errors about dropping const appears anymore.

                Just wasted a whole day by struggling with this bullsh*t

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @MasterQ said in Q_Property and reference data type:

                Why is "const" dropped?

                Because you were trying to return a non-const reference to a const object.

                https://forum.qt.io/topic/113070/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