1.QT中Q_Q和Q_D是什么?
- Q_Q可以得到外部可以访问的类指针
- Q_D可以得到内部封装,外部不可达的类指针
2. 怎么使用?
上代码
#pragma once
#include <QtCore>class A;class APrivate
{Q_DECLARE_PUBLIC(A)
public:APrivate();~APrivate();
public:void APrivateUseA();
private:friend A;A* q_ptr;int m_iA;
};
#pragma once
#include <QtCore>
class APrivate;class A
{Q_DECLARE_PRIVATE(A)
public:A();~A();public://对外接口void setValue(int& iValue);int getValue();public:void APrivateCanUse();void APrivateUse();private:APrivate* d_ptr;
};
#include "APrivate.h"
#include "A.h"
#include <iostream>A::A():d_ptr(new APrivate)
{d_ptr->q_ptr = this;std::cout << "A()" << std::endl;
}
A::~A()
{Q_D(A);delete d;std::cout << "~A()" << std::endl;
}void A::setValue(int& iValue)
{Q_D(A);d->m_iA = iValue;
}int A::getValue()
{Q_D(A);return d->m_iA;
}void A::APrivateCanUse()
{int iValue = 3;setValue(iValue);
}void A::APrivateUse()
{Q_D(A);d->APrivateUseA();
}APrivate::APrivate():m_iA(1)
{std::cout << "APrivate()" << std::e