2013-08-01 45 views
1

我想在Q_D宏的帮助下使用派生类中的d指针。在qt私有类中使用的不完整类型无效

这里是我的父类:

class DIGIKAM_EXPORT GraphicsDImgView : public QGraphicsView 
{ 
    Q_OBJECT 

public: 
    class GraphicsDImgViewPrivate; 

protected: 
    GraphicsDImgViewPrivate* const d_ptr; 

protected: 
    Q_DECLARE_PRIVATE(GraphicsDImgView) 
}; 

和我GraphicsDImgViewPrivate类:

class GraphicsDImgView::GraphicsDImgViewPrivate 
{ 
public: 

    GraphicsDImgViewPrivate() 
    { 
     scene   = 0; 
     item    = 0; 
     layout   = 0; 
     cornerButton  = 0; 
     panIconPopup  = 0; 
     movingInProgress = false; 
     showText   = true; 
    } 

    QGraphicsScene*   scene; 
    GraphicsDImgItem*   item; 
    SinglePhotoPreviewLayout* layout; 

    QToolButton*    cornerButton; 
    KPopupFrame*    panIconPopup; 

    QPoint     mousePressPos; 
    QPoint     panningScrollPos; 
    bool      movingInProgress; 
    bool      showText; 
}; 

GraphicsDImgView::GraphicsDImgView(QWidget* const parent) 
    : QGraphicsView(parent), d_ptr(new GraphicsDImgViewPrivate) 
{ 
    Q_D(GraphicsDImgView); 
    d->scene = new QGraphicsScene(this); 
    d->scene->setItemIndexMethod(QGraphicsScene::NoIndex); 

    setScene(d->scene); 
    d->layout = new SinglePhotoPreviewLayout(this); 
    d->layout->setGraphicsView(this); 
} 

,并在派生类我写了一个方法,我想使用d指针GraphicsDmgView:

bool ImageRegionWidget::movingInProgress() 
{ 
    Q_D(GraphicsDImgView); 
    return d->movingInProgress; 
} 

Howev呃构建给了我以下错误消息

In member function ‘bool Digikam::ImageRegionWidget::movingInProgress()’:...path.... error: invalid use of incomplete type ‘class GraphicsDImgView::GraphicsDImgViewPrivate’

graphicsdimgview.h:128:11: error: forward declaration of ‘class GraphicsDImgView::GraphicsDImgViewPrivate’

我严格遵循的文档,所以我不知道我在哪里出了错。也许我正在粗心大意。请帮我指出我的错误。谢谢:)

+0

快,让完整的代码。看起来你在声明'GraphicsDImgViewPrivate'和include的顺序时有错误。另外...什么是你的“属性”? –

+0

对不起,我会纠正他们... – wceo

回答

3

我不能纠正您的示例中的错误。代码安静肮脏,不清楚。我不知道文章,你会得到这样的推荐。我的建议是做平普尔在明年方式:

MyClass.h

class MyClassPrivate; 
class MyClass 
    : public QObject 
{ 
Q_OBJECT 
public: 
    explicit MyClass(QObject *parent = 0); 
~MyClass() 
protected: 
    MyClassPrivate * const d_ptr; 
    MyClass(MyClassPrivate &dd, QObject * parent); 
private: 
    Q_DECLARE_PRIVATE(MyClass); 
}; 

MyClass_p.h

#include "myclass.h" 

class MyClassPrivate 
{ 
    Q_DECLARE_PUBLIC(MyClass); 
public: 
    MyClassPrivate(); 
    virtual ~MyClassPrivate(); 

    MyClass * q_ptr; 
}; 

MyClass.cpp

#include "myclass.h" 
#include "myclass_p.h" 

MyClassPrivate::MyClassPrivate() 
{} 

MyClassPrivate::~MyClassPrivate() 
{} 

MyClass::MyClass(QObject *parent) 
    :QObject(parent) 
    ,d_ptr(new MyClassPrivate()) 
{ 
    Q_D(MyClass); 
    d->q_ptr = this; 
} 

MyClass::MyClass(MyClassPrivate &dd, QObject * parent) 
    :QObject(parent) 
    ,d_ptr(&dd) 
{ 
    Q_D(MyClass); 
    d->q_ptr = this; 
} 

MyClass::~MyClass() 
{ 
    Q_D(MyClass); 
    delete d; 
} 
+0

感谢您的回答!我想知道构造函数MyClass(MyClassPrivate&dd,QObject * parent)是否可选。另外,我想知道为什么在你使用Q_D(MyClass)的某些方法中使用了d-> q_ptr?非常感谢:) – wceo

+0

q_ptr用于从私有访问公共类(使用宏Q_Q,类似于Q_D)。我用过一次,进行初始化。关于受保护的构造函数-i使用这种方法进行子类化。对于“谢谢”,有特殊的按钮,如“投票”和“接受”;) –

+0

我试过你的代码,但暂时还没有运气......我想我一定已经搞乱了某处... – wceo

相关问题