Hi, today I read a blog post about d-pointers and private class:
http://zchydem.enume.net/2010/01/19/...nd-d-pointers/
I decided to experiment with it. I started creating a standard class, MyWidget:
mywidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
namespace Ui {
class MyWidget;
}
{
Q_OBJECT
public:
explicit MyWidget
(const QString &first,
~MyWidget();
private:
Ui::MyWidget *ui;
};
#endif // MYWIDGET_H
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
namespace Ui {
class MyWidget;
}
class MyWidget : public QWidget
{
Q_OBJECT
public:
explicit MyWidget(const QString &first,
const QString &second,
QWidget *parent = 0);
~MyWidget();
QString first() const;
void setFirst(const QString);
QString second() const;
void setSecond(const QString);
private:
Ui::MyWidget *ui;
QString _first;
QString _second;
};
#endif // MYWIDGET_H
To copy to clipboard, switch view to plain text mode
mywidget.cpp
#include "mywidget.h"
#include "ui_mywidget.h"
ui(new Ui::MyWidget),
_first(first),
_second(second)
{
ui->setupUi(this);
}
MyWidget::~MyWidget()
{
delete ui;
}
{
return _first;
}
void MyWidget
::setFirst(const QString text
) {
_first = text;
}
{
return _second;
}
void MyWidget
::setSecond(const QString text
) {
_second = text;
}
#include "mywidget.h"
#include "ui_mywidget.h"
MyWidget::MyWidget(const QString &first, const QString &second, QWidget *parent) :
QWidget(parent),
ui(new Ui::MyWidget),
_first(first),
_second(second)
{
ui->setupUi(this);
}
MyWidget::~MyWidget()
{
delete ui;
}
QString MyWidget::first() const
{
return _first;
}
void MyWidget::setFirst(const QString text)
{
_first = text;
}
QString MyWidget::second() const
{
return _second;
}
void MyWidget::setSecond(const QString text)
{
_second = text;
}
To copy to clipboard, switch view to plain text mode
then I tryed to implement private class/d-pointers; here it is the final work:
mywidget_p.h
#ifndef MYWIDGET_P_H
#define MYWIDGET_P_H
#include <QWidget>
#include "mywidget.h"
class MyWidgetPrivate : public MyWidget
{
Q_DECLARE_PUBLIC(MyWidget)
public:
MyWidgetPrivate() {}
};
#endif // MYWIDGET_P_H
#ifndef MYWIDGET_P_H
#define MYWIDGET_P_H
#include <QWidget>
#include "mywidget.h"
class MyWidgetPrivate : public MyWidget
{
Q_DECLARE_PUBLIC(MyWidget)
public:
MyWidgetPrivate() {}
QString first;
QString second;
};
#endif // MYWIDGET_P_H
To copy to clipboard, switch view to plain text mode
mywidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
namespace Ui {
class MyWidget;
}
class MyWidgetPrivate;
{
Q_OBJECT
Q_PROPERTY(QString first READ first WRITE setFirst
) Q_PROPERTY(QString second READ second WRITE setSecond
)
public:
explicit MyWidget
(const QString &first,
~MyWidget();
protected:
MyWidgetPrivate * const d;
private:
Ui::MyWidget *ui;
Q_DECLARE_PRIVATE(MyWidget)
};
#endif // MYWIDGET_H
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
namespace Ui {
class MyWidget;
}
class MyWidgetPrivate;
class MyWidget : public QWidget
{
Q_OBJECT
Q_PROPERTY(QString first READ first WRITE setFirst)
Q_PROPERTY(QString second READ second WRITE setSecond)
public:
explicit MyWidget(const QString &first,
const QString &second,
QWidget *parent = 0);
~MyWidget();
QString first() const;
void setFirst(const QString);
QString second() const;
void setSecond(const QString);
protected:
MyWidgetPrivate * const d;
private:
Ui::MyWidget *ui;
Q_DECLARE_PRIVATE(MyWidget)
};
#endif // MYWIDGET_H
To copy to clipboard, switch view to plain text mode
mywidget.cpp
#include "mywidget_p.h"
#include "ui_mywidget.h"
ui(new Ui::MyWidget),
first(first),
second(second)
{
ui->setupUi(this);
}
MyWidget::~MyWidget()
{
delete ui;
}
{
return d->first;
}
void MyWidget
::setFirst(const QString text
) {
Q_D(MyWidget);
d->first = text;
}
{
return d->second;
}
void MyWidget
::setSecond(const QString text
) {
Q_D(MyWidget);
d->second = text;
}
#include "mywidget_p.h"
#include "ui_mywidget.h"
MyWidget::MyWidget(const QString &first, const QString &second, QWidget *parent) :
QWidget(parent),
ui(new Ui::MyWidget),
first(first),
second(second)
{
ui->setupUi(this);
}
MyWidget::~MyWidget()
{
delete ui;
}
QString MyWidget::first() const
{
return d->first;
}
void MyWidget::setFirst(const QString text)
{
Q_D(MyWidget);
d->first = text;
}
QString MyWidget::second() const
{
return d->second;
}
void MyWidget::setSecond(const QString text)
{
Q_D(MyWidget);
d->second = text;
}
To copy to clipboard, switch view to plain text mode
compiling, I got
In file included from mywidget.cpp:1:0:
mywidget_p.h: In member function ‘MyWidget* MyWidgetPrivate::q_func()’:
mywidget_p.h:10:5: error: ‘q_ptr’ was not declared in this scope
mywidget_p.h: In member function ‘const MyWidget* MyWidgetPrivate::q_func() const’:
mywidget_p.h:10:5: error: ‘q_ptr’ was not declared in this scope
mywidget_p.h: In constructor ‘MyWidgetPrivate::MyWidgetPrivate()’:
mywidget_p.h:13:23: error: no matching function for call to ‘MyWidget::MyWidget()’
mywidget_p.h:13:23: note: candidates are:
In file included from mywidget_p.h:6:0,
from mywidget.cpp:1:
mywidget.
h:19:14: note
: MyWidget
::MyWidget(const QString
&,
const QString
&,
QWidget*)mywidget.h:19:14: note: candidate expects 3 arguments, 0 provided
mywidget.h:12:7: note: MyWidget::MyWidget(const MyWidget&)
mywidget.h:12:7: note: candidate expects 1 argument, 0 provided
mywidget.
cpp: In constructor ‘MyWidget
::MyWidget(const QString
&,
const QString
&,
QWidget*)’
:mywidget.cpp:7:5: error: class ‘MyWidget’ does not have any field named ‘first’
mywidget.cpp:8:5: error: class ‘MyWidget’ does not have any field named ‘second’
mywidget.cpp:4:1: error: uninitialized member ‘MyWidget::d’ with ‘const’ type ‘MyWidgetPrivate* const’ [-fpermissive]
make: *** [mywidget.o] Errore 1
In file included from mywidget.cpp:1:0:
mywidget_p.h: In member function ‘MyWidget* MyWidgetPrivate::q_func()’:
mywidget_p.h:10:5: error: ‘q_ptr’ was not declared in this scope
mywidget_p.h: In member function ‘const MyWidget* MyWidgetPrivate::q_func() const’:
mywidget_p.h:10:5: error: ‘q_ptr’ was not declared in this scope
mywidget_p.h: In constructor ‘MyWidgetPrivate::MyWidgetPrivate()’:
mywidget_p.h:13:23: error: no matching function for call to ‘MyWidget::MyWidget()’
mywidget_p.h:13:23: note: candidates are:
In file included from mywidget_p.h:6:0,
from mywidget.cpp:1:
mywidget.h:19:14: note: MyWidget::MyWidget(const QString&, const QString&, QWidget*)
mywidget.h:19:14: note: candidate expects 3 arguments, 0 provided
mywidget.h:12:7: note: MyWidget::MyWidget(const MyWidget&)
mywidget.h:12:7: note: candidate expects 1 argument, 0 provided
mywidget.cpp: In constructor ‘MyWidget::MyWidget(const QString&, const QString&, QWidget*)’:
mywidget.cpp:7:5: error: class ‘MyWidget’ does not have any field named ‘first’
mywidget.cpp:8:5: error: class ‘MyWidget’ does not have any field named ‘second’
mywidget.cpp:4:1: error: uninitialized member ‘MyWidget::d’ with ‘const’ type ‘MyWidgetPrivate* const’ [-fpermissive]
make: *** [mywidget.o] Errore 1
To copy to clipboard, switch view to plain text mode
Where am I wrong?
Thanks.
Bookmarks