Q_D macro not working
-
@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 mySecondClass
- It calls the
q->printMethod()
which tends to access the intd->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.
- I'm calling the
-
@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 mySecondClass
- It calls the
q->printMethod()
which tends to access the intd->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-Ehrlicher
Hey Christian, here's the issue I'm getting.// Does not work
Q_D(SecondClass);
n = d->etaD;
// Works
n = 10;
- I'm calling the
-
@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 mySecondClass
- It calls the
q->printMethod()
which tends to access the intd->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-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?
- I'm calling the
-
@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?
Also Christian,
I'd be very helpful of you if you could explain to me when I can use theconst
keyword in theQ_D
and theQ_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(); | ~~~~~~~~~~~~~~~~~~~~~^~
-
Also Christian,
I'd be very helpful of you if you could explain to me when I can use theconst
keyword in theQ_D
and theQ_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(); | ~~~~~~~~~~~~~~~~~~~~~^~
@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.
-
@Christian-Ehrlicher
Hey Christian, here's the issue I'm getting.// Does not work
Q_D(SecondClass);
n = d->etaD;
// Works
n = 10;
@Christian-Ehrlicher
And this doubt Christian ?
The first one.See, I've uploaded the output screenshots.
-
@Christian-Ehrlicher
And this doubt Christian ?
The first one.See, I've uploaded the output screenshots.
@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 theQ_D
andd_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.