[Interest] correct d_ptr implementation

Graham Labdon Graham.Labdon at avalonsciences.com
Mon Nov 11 15:40:26 CET 2013


Hi
I am developing a library and in Qt Tradition I want to use the d_ptr pattern.
I have no previous experience of using this pattern and have a simple example working but wanted to check that my implementation is correct.
To that end I have set out my classes below and would be grateful if anyone could confirm that my approach is correct(or not)

Thanks

Header file

#ifndef DISPLAYWIDGET_H
#define DISPLAYWIDGET_H
#include "DisplayWidgetsGlobal.h"
#include <QWidget>

class DisplayWidgetPrivate;

class DISPLAYWIDGETS_EXPORT DisplayWidget : public QWidget
{
        Q_OBJECT

public:
        DisplayWidget(QWidget *parent);
        ~DisplayWidget();

private:
        DisplayWidgetPrivate* d_ptr;
        Q_DECLARE_PRIVATE(DisplayWidget)
};

#endif // DISPLAYWIDGET_H


Private Header file

#include "DisplayWidget.h"

class QLabel;
class DisplayWidgetPrivate
{
public:
        DisplayWidgetPrivate (DisplayWidget* parent);
        void init();

        QLabel* m_label;
        DisplayWidget* const q_ptr;
        Q_DECLARE_PUBLIC(DisplayWidget)

public:
        DisplayWidgetPrivate();
};


Implementation file

#include <QLabel>
#include "DisplayWidget.h"
#include "DisplayWidget_p.h"

DisplayWidgetPrivate::DisplayWidgetPrivate(DisplayWidget* parent)
        : q_ptr(parent)
{

}

void DisplayWidgetPrivate::init()
{
        m_label = new QLabel("This is a label",q_ptr);
}

DisplayWidget::DisplayWidget(QWidget *parent)
        : QWidget(parent),
          d_ptr(new DisplayWidgetPrivate(this))
{
        Q_D(DisplayWidget);
        d->init();
}

DisplayWidget::~DisplayWidget()
{

}



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20131111/f82587d6/attachment.html>


More information about the Interest mailing list