Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Correct us of d_ptr pattern
Forum Updated to NodeBB v4.3 + New Features

Correct us of d_ptr pattern

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.1k Views 1 Watching
  • 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.
  • G Offline
    G Offline
    GrahamL
    wrote on last edited by
    #1

    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()
    {

    }
    @

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      On a quick look, it looks good.

      (Z(:^

      1 Reply Last reply
      0
      • G Offline
        G Offline
        GrahamL
        wrote on last edited by
        #3

        Thanks - good to hear

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on last edited by
          #4

          Hi,

          to read more about d-pointer you can see "here":qt-project.org/wiki/Dpointer.

          In your code I found only a problem, you don't have to define d_ptr and q_ptr because they are defined in QObject and QObjectPrivate classes; if you class inherits (directly or not) from QObject you don't have to define them.

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply
          0
          • G Offline
            G Offline
            GrahamL
            wrote on last edited by
            #5

            Thanks mcosta
            But if I remove the declarations for q_ptr and d_ptr the compilation fails

            @
            DisplayWidget.cpp(11): error C2614: 'DisplayWidgetPrivate' : illegal member initialization: 'q_ptr' is not a base or member
            @

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mcosta
              wrote on last edited by
              #6

              Hi,

              sorry MY ERROR , q_ptr is avalable only if your *Private Class inherits from QObjectPrivate.

              Once your problem is solved don't forget to:

              • Mark the thread as SOLVED using the Topic Tool menu
              • Vote up the answer(s) that helped you to solve the issue

              You can embed images using (http://imgur.com/) or (http://postimage.org/)

              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