Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. Q_D macro not working
Forum Updated to NodeBB v4.3 + New Features

Q_D macro not working

Scheduled Pinned Locked Moved Unsolved Qt 6
7 Posts 3 Posters 795 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.
  • Ash VA Offline
    Ash VA Offline
    Ash V
    wrote on last edited by Ash V
    #1

    @Axel-Spoerl
    Hey Axel !
    I sincerely hope you're having a great day.

    I've come across this issue today in my Qt Creator, that the Q_D macro is not able to fetch data from my private class.

    Specifically,

    • I'm calling the d->privatePrintMethod() in the constructor of my SecondClass
    • It calls the q->printMethod() which tends to access the int d->etaD but is not able to get it

    dqpointer.hpp

    #ifndef DQPOINTER_H
    #define DQPOINTER_H
    
    #include <QDebug>
    
    class FirstClassPrivate;
    class FirstClass
    {
    public:
        FirstClass();
    
    protected:
        FirstClass(FirstClassPrivate* d);
        FirstClassPrivate* d_ptr;
    private:
        Q_DECLARE_PRIVATE(FirstClass)
    };
    
    
    class SecondClassPrivate;
    class SecondClass : public FirstClass
    {
    public:
        SecondClass();
        void printMethod();
    
    protected:
        SecondClass(SecondClassPrivate* d);
    private:
        Q_DECLARE_PRIVATE(SecondClass)
    };
    
    #endif // DQPOINTER_H
    

    dqpointer_p.hpp

    #ifndef DQPOINTER_P_H
    #define DQPOINTER_P_H
    
    class FirstClass;
    class FirstClassPrivate
    {
    public:
        FirstClassPrivate()
        {
            qDebug() << "FirstClassPrivate Constructor 1\n";
        }
    
        FirstClassPrivate(FirstClass* q)
            : q_ptr(q)
        {
            qDebug() << "FirstClassPrivate Constructor 2\n";
        }
    
        bool etaA;
        char etaB;
    
    protected:
        FirstClass* q_ptr;
    private:
        Q_DECLARE_PUBLIC(FirstClass)
    };
    
    
    class SecondClass;
    class SecondClassPrivate : public FirstClassPrivate
    {
    public:
        SecondClassPrivate()
        {
            qDebug() << "SecondClassPrivate Constructor 1\n";
        }
    
        SecondClassPrivate(SecondClass* q)
            : FirstClassPrivate(q)
        {
            qDebug() << "SecondClassPrivate Constructor 2\n";
        }
    
        void privatePrintMethod()
        {
            Q_Q(SecondClass);
            q->printMethod();
        }
    
        float etaC;
        int etaD = 10;
    
    private:
        Q_DECLARE_PUBLIC(SecondClass)
    };
    
    #endif // DQPOINTER_P_H
    

    dqpointer.cpp

    #include "dqpointer.h"
    #include "dqpointer_p.h"
    
    FirstClass::FirstClass()
        : d_ptr(new FirstClassPrivate(this))
    {
        qDebug() << "FirstClass Constructor 1\n";
    }
    
    FirstClass::FirstClass(FirstClassPrivate* d)
        : d_ptr(d)
    {
        qDebug() << "FirstClass Constructor 2\n";
    }
    
    
    SecondClass::SecondClass()
        : FirstClass(new SecondClassPrivate)
    {
        qDebug() << "SecondClass Constructor 1\n";
    
        Q_D(SecondClass);
        d->privatePrintMethod();
    }
    
    SecondClass::SecondClass(SecondClassPrivate* d)
        : FirstClass(d)
    {
        qDebug() << "SecondClass Constructor 2\n";
    }
    
    void SecondClass::printMethod()
    {
        int n;
    
        // Does not work
        // Q_D(SecondClass);
        // n = d->etaD;
    
        // Works
        n = 10;
    
        for(int i = 1; i <= n; i++)
            qDebug() << i;
    }
    

    main.cpp

    #include <QCoreApplication>
    #include <QDebug>
    
    #include "dqpointer.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        SecondClass* s = new SecondClass;
    
        return a.exec();
    }
    

    It'd be a great help from your side if you could help me out in this issue.

    Ash VA Christian EhrlicherC 2 Replies Last reply
    0
    • Ash VA Ash V

      @Axel-Spoerl
      Hey Axel !
      I sincerely hope you're having a great day.

      I've come across this issue today in my Qt Creator, that the Q_D macro is not able to fetch data from my private class.

      Specifically,

      • I'm calling the d->privatePrintMethod() in the constructor of my SecondClass
      • It calls the q->printMethod() which tends to access the int d->etaD but is not able to get it

      dqpointer.hpp

      #ifndef DQPOINTER_H
      #define DQPOINTER_H
      
      #include <QDebug>
      
      class FirstClassPrivate;
      class FirstClass
      {
      public:
          FirstClass();
      
      protected:
          FirstClass(FirstClassPrivate* d);
          FirstClassPrivate* d_ptr;
      private:
          Q_DECLARE_PRIVATE(FirstClass)
      };
      
      
      class SecondClassPrivate;
      class SecondClass : public FirstClass
      {
      public:
          SecondClass();
          void printMethod();
      
      protected:
          SecondClass(SecondClassPrivate* d);
      private:
          Q_DECLARE_PRIVATE(SecondClass)
      };
      
      #endif // DQPOINTER_H
      

      dqpointer_p.hpp

      #ifndef DQPOINTER_P_H
      #define DQPOINTER_P_H
      
      class FirstClass;
      class FirstClassPrivate
      {
      public:
          FirstClassPrivate()
          {
              qDebug() << "FirstClassPrivate Constructor 1\n";
          }
      
          FirstClassPrivate(FirstClass* q)
              : q_ptr(q)
          {
              qDebug() << "FirstClassPrivate Constructor 2\n";
          }
      
          bool etaA;
          char etaB;
      
      protected:
          FirstClass* q_ptr;
      private:
          Q_DECLARE_PUBLIC(FirstClass)
      };
      
      
      class SecondClass;
      class SecondClassPrivate : public FirstClassPrivate
      {
      public:
          SecondClassPrivate()
          {
              qDebug() << "SecondClassPrivate Constructor 1\n";
          }
      
          SecondClassPrivate(SecondClass* q)
              : FirstClassPrivate(q)
          {
              qDebug() << "SecondClassPrivate Constructor 2\n";
          }
      
          void privatePrintMethod()
          {
              Q_Q(SecondClass);
              q->printMethod();
          }
      
          float etaC;
          int etaD = 10;
      
      private:
          Q_DECLARE_PUBLIC(SecondClass)
      };
      
      #endif // DQPOINTER_P_H
      

      dqpointer.cpp

      #include "dqpointer.h"
      #include "dqpointer_p.h"
      
      FirstClass::FirstClass()
          : d_ptr(new FirstClassPrivate(this))
      {
          qDebug() << "FirstClass Constructor 1\n";
      }
      
      FirstClass::FirstClass(FirstClassPrivate* d)
          : d_ptr(d)
      {
          qDebug() << "FirstClass Constructor 2\n";
      }
      
      
      SecondClass::SecondClass()
          : FirstClass(new SecondClassPrivate)
      {
          qDebug() << "SecondClass Constructor 1\n";
      
          Q_D(SecondClass);
          d->privatePrintMethod();
      }
      
      SecondClass::SecondClass(SecondClassPrivate* d)
          : FirstClass(d)
      {
          qDebug() << "SecondClass Constructor 2\n";
      }
      
      void SecondClass::printMethod()
      {
          int n;
      
          // Does not work
          // Q_D(SecondClass);
          // n = d->etaD;
      
          // Works
          n = 10;
      
          for(int i = 1; i <= n; i++)
              qDebug() << i;
      }
      

      main.cpp

      #include <QCoreApplication>
      #include <QDebug>
      
      #include "dqpointer.h"
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
      
          SecondClass* s = new SecondClass;
      
          return a.exec();
      }
      

      It'd be a great help from your side if you could help me out in this issue.

      Ash VA Offline
      Ash VA Offline
      Ash V
      wrote on last edited by Ash V
      #2

      @Christian-Ehrlicher
      Hey Christian, here's the issue I'm getting.

      // Does not work
      Q_D(SecondClass);
      n = d->etaD;
      573a0dab-0c9a-490a-8229-4a62175c9e36-Screenshot from 2024-03-06 12-43-46.png
      // Works
      n = 10;
      ae5c2d81-c6a8-4239-8039-6e45cdb57016-Screenshot from 2024-03-06 12-44-16.png

      Ash VA 1 Reply Last reply
      0
      • Ash VA Ash V

        @Axel-Spoerl
        Hey Axel !
        I sincerely hope you're having a great day.

        I've come across this issue today in my Qt Creator, that the Q_D macro is not able to fetch data from my private class.

        Specifically,

        • I'm calling the d->privatePrintMethod() in the constructor of my SecondClass
        • It calls the q->printMethod() which tends to access the int d->etaD but is not able to get it

        dqpointer.hpp

        #ifndef DQPOINTER_H
        #define DQPOINTER_H
        
        #include <QDebug>
        
        class FirstClassPrivate;
        class FirstClass
        {
        public:
            FirstClass();
        
        protected:
            FirstClass(FirstClassPrivate* d);
            FirstClassPrivate* d_ptr;
        private:
            Q_DECLARE_PRIVATE(FirstClass)
        };
        
        
        class SecondClassPrivate;
        class SecondClass : public FirstClass
        {
        public:
            SecondClass();
            void printMethod();
        
        protected:
            SecondClass(SecondClassPrivate* d);
        private:
            Q_DECLARE_PRIVATE(SecondClass)
        };
        
        #endif // DQPOINTER_H
        

        dqpointer_p.hpp

        #ifndef DQPOINTER_P_H
        #define DQPOINTER_P_H
        
        class FirstClass;
        class FirstClassPrivate
        {
        public:
            FirstClassPrivate()
            {
                qDebug() << "FirstClassPrivate Constructor 1\n";
            }
        
            FirstClassPrivate(FirstClass* q)
                : q_ptr(q)
            {
                qDebug() << "FirstClassPrivate Constructor 2\n";
            }
        
            bool etaA;
            char etaB;
        
        protected:
            FirstClass* q_ptr;
        private:
            Q_DECLARE_PUBLIC(FirstClass)
        };
        
        
        class SecondClass;
        class SecondClassPrivate : public FirstClassPrivate
        {
        public:
            SecondClassPrivate()
            {
                qDebug() << "SecondClassPrivate Constructor 1\n";
            }
        
            SecondClassPrivate(SecondClass* q)
                : FirstClassPrivate(q)
            {
                qDebug() << "SecondClassPrivate Constructor 2\n";
            }
        
            void privatePrintMethod()
            {
                Q_Q(SecondClass);
                q->printMethod();
            }
        
            float etaC;
            int etaD = 10;
        
        private:
            Q_DECLARE_PUBLIC(SecondClass)
        };
        
        #endif // DQPOINTER_P_H
        

        dqpointer.cpp

        #include "dqpointer.h"
        #include "dqpointer_p.h"
        
        FirstClass::FirstClass()
            : d_ptr(new FirstClassPrivate(this))
        {
            qDebug() << "FirstClass Constructor 1\n";
        }
        
        FirstClass::FirstClass(FirstClassPrivate* d)
            : d_ptr(d)
        {
            qDebug() << "FirstClass Constructor 2\n";
        }
        
        
        SecondClass::SecondClass()
            : FirstClass(new SecondClassPrivate)
        {
            qDebug() << "SecondClass Constructor 1\n";
        
            Q_D(SecondClass);
            d->privatePrintMethod();
        }
        
        SecondClass::SecondClass(SecondClassPrivate* d)
            : FirstClass(d)
        {
            qDebug() << "SecondClass Constructor 2\n";
        }
        
        void SecondClass::printMethod()
        {
            int n;
        
            // Does not work
            // Q_D(SecondClass);
            // n = d->etaD;
        
            // Works
            n = 10;
        
            for(int i = 1; i <= n; i++)
                qDebug() << i;
        }
        

        main.cpp

        #include <QCoreApplication>
        #include <QDebug>
        
        #include "dqpointer.h"
        
        int main(int argc, char *argv[])
        {
            QCoreApplication a(argc, argv);
        
            SecondClass* s = new SecondClass;
        
            return a.exec();
        }
        

        It'd be a great help from your side if you could help me out in this issue.

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Ash-V said in Q_D macro not working:

        but is not able to get it

        What does it mean? Do you get anything compiler error or do you don't get the expected value?

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        Ash VA 1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          @Ash-V said in Q_D macro not working:

          but is not able to get it

          What does it mean? Do you get anything compiler error or do you don't get the expected value?

          Ash VA Offline
          Ash VA Offline
          Ash V
          wrote on last edited by Ash V
          #4

          @Christian-Ehrlicher

          Also Christian,
          I'd be very helpful of you if you could explain to me when I can use the const keyword in the Q_D and the Q_Q macros.

          I used const in Q_D() like this,

          SecondClass::SecondClass()
              : FirstClass(new SecondClassPrivate)
          {
              qDebug() << "SecondClass Constructor 1\n";
          
              Q_D(const SecondClass);
              d->privatePrintMethod();
          }
          

          but it throws this error.

          /dqpointer.cpp: error: passing ‘const SecondClassPrivate’ as ‘this’ argument discards qualifiers [-fpermissive]
          /dqpointer.cpp: In constructor ‘SecondClass::SecondClass()’:
          /dqpointer.cpp: error: passing ‘const SecondClassPrivate’ as ‘this’ argument discards qualifiers [-fpermissive]
                |     d->privatePrintMethod();
                |     ~~~~~~~~~~~~~~~~~~~~~^~
          
          Christian EhrlicherC 1 Reply Last reply
          0
          • Ash VA Ash V

            @Christian-Ehrlicher

            Also Christian,
            I'd be very helpful of you if you could explain to me when I can use the const keyword in the Q_D and the Q_Q macros.

            I used const in Q_D() like this,

            SecondClass::SecondClass()
                : FirstClass(new SecondClassPrivate)
            {
                qDebug() << "SecondClass Constructor 1\n";
            
                Q_D(const SecondClass);
                d->privatePrintMethod();
            }
            

            but it throws this error.

            /dqpointer.cpp: error: passing ‘const SecondClassPrivate’ as ‘this’ argument discards qualifiers [-fpermissive]
            /dqpointer.cpp: In constructor ‘SecondClass::SecondClass()’:
            /dqpointer.cpp: error: passing ‘const SecondClassPrivate’ as ‘this’ argument discards qualifiers [-fpermissive]
                  |     d->privatePrintMethod();
                  |     ~~~~~~~~~~~~~~~~~~~~~^~
            
            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Ash-V said in Q_D macro not working:

            const keyword in the Q_D and the Q_Q macros

            What has this to do with Qt? You can not call a non-const function from a const object. Make your function const.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0
            • Ash VA Ash V

              @Christian-Ehrlicher
              Hey Christian, here's the issue I'm getting.

              // Does not work
              Q_D(SecondClass);
              n = d->etaD;
              573a0dab-0c9a-490a-8229-4a62175c9e36-Screenshot from 2024-03-06 12-43-46.png
              // Works
              n = 10;
              ae5c2d81-c6a8-4239-8039-6e45cdb57016-Screenshot from 2024-03-06 12-44-16.png

              Ash VA Offline
              Ash VA Offline
              Ash V
              wrote on last edited by Ash V
              #6

              @Christian-Ehrlicher
              And this doubt Christian ?
              The first one.

              See, I've uploaded the output screenshots.

              JonBJ 1 Reply Last reply
              0
              • Ash VA Ash V

                @Christian-Ehrlicher
                And this doubt Christian ?
                The first one.

                See, I've uploaded the output screenshots.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @Ash-V
                May I ask: I thought you were a "beginner" at Qt, maybe C++ too (but perhaps not). If you know what you are doing, and have a good usage case, using all the Q_D and d_ptr stuff may be fine. But I, and most people, have never used them, so I just wonder whether you might be over-complicating things.

                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