Thank you anda_skoam, your last tips solved all problems.
I have one more question: d-pointers should be limited to libraries or is good habit to use them in normal apps, too?
Regards.
P.S.
Here it is final code, if useful to others:
mywidget_p.h
#ifndef MYWIDGET_P_H
#define MYWIDGET_P_H
#include "mywidget.h"
#include "ui_mywidget.h"
namespace Ui {
class MyWidget;
}
class MyWidgetPrivate
{
MyWidget * const q; // not stricly necessary, just in case you put methods into MyWidgetPrivate that need access to MyWidget
public:
explicit MyWidgetPrivate( MyWidget *parent ) :
q( parent ), ui( new Ui::MyWidget )
{
ui->setupUi( parent );
}
Ui::MyWidget *ui;
};
#endif // MYWIDGET_P_H
#ifndef MYWIDGET_P_H
#define MYWIDGET_P_H
#include "mywidget.h"
#include "ui_mywidget.h"
namespace Ui {
class MyWidget;
}
class MyWidgetPrivate
{
MyWidget * const q; // not stricly necessary, just in case you put methods into MyWidgetPrivate that need access to MyWidget
public:
explicit MyWidgetPrivate( MyWidget *parent ) :
q( parent ), ui( new Ui::MyWidget )
{
ui->setupUi( parent );
}
Ui::MyWidget *ui;
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>
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();
private:
MyWidgetPrivate * const d;
};
#endif // MYWIDGET_H
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
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);
private:
MyWidgetPrivate * const d;
};
#endif // MYWIDGET_H
To copy to clipboard, switch view to plain text mode
mywidget.cpp
#include "mywidget_p.h"
d( new MyWidgetPrivate( this ) )
{
d->first = first;
d->second = second;
}
MyWidget::~MyWidget()
{
delete d;
}
{
return d->first;
}
void MyWidget
::setFirst(const QString text
) {
d->first = text;
}
{
return d->second;
}
void MyWidget
::setSecond(const QString text
) {
d->second = text;
}
#include "mywidget_p.h"
MyWidget::MyWidget(const QString &first, const QString &second, QWidget *parent) :
QWidget(parent),
d( new MyWidgetPrivate( this ) )
{
d->first = first;
d->second = second;
}
MyWidget::~MyWidget()
{
delete d;
}
QString MyWidget::first() const
{
return d->first;
}
void MyWidget::setFirst(const QString text)
{
d->first = text;
}
QString MyWidget::second() const
{
return d->second;
}
void MyWidget::setSecond(const QString text)
{
d->second = text;
}
To copy to clipboard, switch view to plain text mode
Bookmarks