QtWidgets: Introduce delegating constructors.

Reduce code duplication by chaining constructors.

Change-Id: I0229556a417153063ac6d14d35765c85e6fe1fe8
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
Friedemann Kleint 2016-04-29 15:12:22 +02:00
parent 799be4e489
commit f40dbe0d0b
23 changed files with 33 additions and 100 deletions

View File

@ -1948,10 +1948,8 @@ static const Qt::WindowFlags DefaultWindowFlags =
Constructs a color dialog with the given \a parent. Constructs a color dialog with the given \a parent.
*/ */
QColorDialog::QColorDialog(QWidget *parent) QColorDialog::QColorDialog(QWidget *parent)
: QDialog(*new QColorDialogPrivate, parent, DefaultWindowFlags) : QColorDialog(QColor(Qt::white), parent)
{ {
Q_D(QColorDialog);
d->init(Qt::white);
} }
/*! /*!

View File

@ -168,10 +168,8 @@ QFontDialog::QFontDialog(QWidget *parent)
\a initial color. \a initial color.
*/ */
QFontDialog::QFontDialog(const QFont &initial, QWidget *parent) QFontDialog::QFontDialog(const QFont &initial, QWidget *parent)
: QDialog(*new QFontDialogPrivate, parent, DefaultWindowFlags) : QFontDialog(parent)
{ {
Q_D(QFontDialog);
d->init();
setCurrentFont(initial); setCurrentFont(initial);
} }

View File

@ -279,12 +279,8 @@ void QActionPrivate::setShortcutEnabled(bool enable, QShortcutMap &map)
group the action will be automatically inserted into the group. group the action will be automatically inserted into the group.
*/ */
QAction::QAction(QObject* parent) QAction::QAction(QObject* parent)
: QObject(*(new QActionPrivate), parent) : QAction(*new QActionPrivate, parent)
{ {
Q_D(QAction);
d->group = qobject_cast<QActionGroup *>(parent);
if (d->group)
d->group->addAction(this);
} }
@ -302,13 +298,10 @@ QAction::QAction(QObject* parent)
*/ */
QAction::QAction(const QString &text, QObject* parent) QAction::QAction(const QString &text, QObject* parent)
: QObject(*(new QActionPrivate), parent) : QAction(parent)
{ {
Q_D(QAction); Q_D(QAction);
d->text = text; d->text = text;
d->group = qobject_cast<QActionGroup *>(parent);
if (d->group)
d->group->addAction(this);
} }
/*! /*!
@ -324,14 +317,10 @@ QAction::QAction(const QString &text, QObject* parent)
setToolTip(). setToolTip().
*/ */
QAction::QAction(const QIcon &icon, const QString &text, QObject* parent) QAction::QAction(const QIcon &icon, const QString &text, QObject* parent)
: QObject(*(new QActionPrivate), parent) : QAction(text, parent)
{ {
Q_D(QAction); Q_D(QAction);
d->icon = icon; d->icon = icon;
d->text = text;
d->group = qobject_cast<QActionGroup *>(parent);
if (d->group)
d->group->addAction(this);
} }
/*! /*!

View File

@ -456,12 +456,11 @@ QShortcut::QShortcut(QWidget *parent)
QShortcut::QShortcut(const QKeySequence &key, QWidget *parent, QShortcut::QShortcut(const QKeySequence &key, QWidget *parent,
const char *member, const char *ambiguousMember, const char *member, const char *ambiguousMember,
Qt::ShortcutContext context) Qt::ShortcutContext context)
: QObject(*new QShortcutPrivate, parent) : QShortcut(parent)
{ {
QAPP_CHECK("QShortcut"); QAPP_CHECK("QShortcut");
Q_D(QShortcut); Q_D(QShortcut);
Q_ASSERT(parent != 0);
d->sc_context = context; d->sc_context = context;
d->sc_sequence = key; d->sc_sequence = key;
d->redoGrab(qApp->d_func()->shortcutMap); d->redoGrab(qApp->d_func()->shortcutMap);

View File

@ -131,16 +131,9 @@ QProxyStyle::QProxyStyle(QStyle *style) :
\sa QStyleFactory::create() \sa QStyleFactory::create()
*/ */
QProxyStyle::QProxyStyle(const QString &key) : QProxyStyle::QProxyStyle(const QString &key)
QCommonStyle(*new QProxyStylePrivate()) : QProxyStyle(QStyleFactory::create(key))
{ {
Q_D(QProxyStyle);
QStyle *style = QStyleFactory::create(key);
if (style) {
d->baseStyle = style;
style->setProxy(this);
style->setParent(this); // Take ownership
}
} }
/*! /*!

View File

@ -141,7 +141,7 @@ QSystemTrayIcon::QSystemTrayIcon(QObject *parent)
\sa visible \sa visible
*/ */
QSystemTrayIcon::QSystemTrayIcon(const QIcon &icon, QObject *parent) QSystemTrayIcon::QSystemTrayIcon(const QIcon &icon, QObject *parent)
: QObject(*new QSystemTrayIconPrivate(), parent) : QSystemTrayIcon(parent)
{ {
setIcon(icon); setIcon(icon);
} }

View File

@ -110,10 +110,8 @@ QT_BEGIN_NAMESPACE
*/ */
QUndoCommand::QUndoCommand(const QString &text, QUndoCommand *parent) QUndoCommand::QUndoCommand(const QString &text, QUndoCommand *parent)
: QUndoCommand(parent)
{ {
d = new QUndoCommandPrivate;
if (parent != 0)
parent->d->child_list.append(this);
setText(text); setText(text);
} }

View File

@ -204,10 +204,8 @@ QCheckBox::QCheckBox(QWidget *parent)
*/ */
QCheckBox::QCheckBox(const QString &text, QWidget *parent) QCheckBox::QCheckBox(const QString &text, QWidget *parent)
: QAbstractButton (*new QCheckBoxPrivate, parent) : QCheckBox(parent)
{ {
Q_D(QCheckBox);
d->init();
setText(text); setText(text);
} }

View File

@ -281,23 +281,18 @@ QCommandLinkButton::QCommandLinkButton(QWidget *parent)
*/ */
QCommandLinkButton::QCommandLinkButton(const QString &text, QWidget *parent) QCommandLinkButton::QCommandLinkButton(const QString &text, QWidget *parent)
: QPushButton(*new QCommandLinkButtonPrivate, parent) : QCommandLinkButton(parent)
{ {
Q_D(QCommandLinkButton);
setText(text); setText(text);
d->init();
} }
/*! /*!
Constructs a command link with a \a text, a \a description, and a \a parent. Constructs a command link with a \a text, a \a description, and a \a parent.
*/ */
QCommandLinkButton::QCommandLinkButton(const QString &text, const QString &description, QWidget *parent) QCommandLinkButton::QCommandLinkButton(const QString &text, const QString &description, QWidget *parent)
: QPushButton(*new QCommandLinkButtonPrivate, parent) : QCommandLinkButton(text, parent)
{ {
Q_D(QCommandLinkButton);
setText(text);
setDescription(description); setDescription(description);
d->init();
} }
/*! /*!

View File

@ -462,9 +462,8 @@ void QDialogButtonBoxPrivate::retranslateStrings()
\sa orientation, addButton() \sa orientation, addButton()
*/ */
QDialogButtonBox::QDialogButtonBox(QWidget *parent) QDialogButtonBox::QDialogButtonBox(QWidget *parent)
: QWidget(*new QDialogButtonBoxPrivate(Qt::Horizontal), parent, 0) : QDialogButtonBox(Qt::Horizontal, parent)
{ {
d_func()->initLayout();
} }
/*! /*!
@ -487,10 +486,8 @@ QDialogButtonBox::QDialogButtonBox(Qt::Orientation orientation, QWidget *parent)
\sa orientation, addButton() \sa orientation, addButton()
*/ */
QDialogButtonBox::QDialogButtonBox(StandardButtons buttons, QWidget *parent) QDialogButtonBox::QDialogButtonBox(StandardButtons buttons, QWidget *parent)
: QWidget(*new QDialogButtonBoxPrivate(Qt::Horizontal), parent, 0) : QDialogButtonBox(buttons, Qt::Horizontal, parent)
{ {
d_func()->initLayout();
d_func()->createStandardButtons(buttons);
} }
/*! /*!
@ -501,9 +498,8 @@ QDialogButtonBox::QDialogButtonBox(StandardButtons buttons, QWidget *parent)
*/ */
QDialogButtonBox::QDialogButtonBox(StandardButtons buttons, Qt::Orientation orientation, QDialogButtonBox::QDialogButtonBox(StandardButtons buttons, Qt::Orientation orientation,
QWidget *parent) QWidget *parent)
: QWidget(*new QDialogButtonBoxPrivate(orientation), parent, 0) : QDialogButtonBox(orientation, parent)
{ {
d_func()->initLayout();
d_func()->createStandardButtons(buttons); d_func()->createStandardButtons(buttons);
} }

View File

@ -1224,10 +1224,8 @@ QDockWidget::QDockWidget(QWidget *parent, Qt::WindowFlags flags)
\sa setWindowTitle() \sa setWindowTitle()
*/ */
QDockWidget::QDockWidget(const QString &title, QWidget *parent, Qt::WindowFlags flags) QDockWidget::QDockWidget(const QString &title, QWidget *parent, Qt::WindowFlags flags)
: QWidget(*new QDockWidgetPrivate, parent, flags) : QDockWidget(parent, flags)
{ {
Q_D(QDockWidget);
d->init();
setWindowTitle(title); setWindowTitle(title);
} }

View File

@ -194,10 +194,8 @@ QGroupBox::QGroupBox(QWidget *parent)
*/ */
QGroupBox::QGroupBox(const QString &title, QWidget *parent) QGroupBox::QGroupBox(const QString &title, QWidget *parent)
: QWidget(*new QGroupBoxPrivate, parent, 0) : QGroupBox(parent)
{ {
Q_D(QGroupBox);
d->init();
setTitle(title); setTitle(title);
} }

View File

@ -138,21 +138,17 @@ void QKeySequenceEditPrivate::finishEditing()
/*! /*!
Constructs a QKeySequenceEdit widget with the given \a parent. Constructs a QKeySequenceEdit widget with the given \a parent.
*/ */
QKeySequenceEdit::QKeySequenceEdit(QWidget *parent) : QKeySequenceEdit::QKeySequenceEdit(QWidget *parent)
QWidget(*new QKeySequenceEditPrivate, parent, 0) : QKeySequenceEdit(*new QKeySequenceEditPrivate, parent, 0)
{ {
Q_D(QKeySequenceEdit);
d->init();
} }
/*! /*!
Constructs a QKeySequenceEdit widget with the given \a keySequence and \a parent. Constructs a QKeySequenceEdit widget with the given \a keySequence and \a parent.
*/ */
QKeySequenceEdit::QKeySequenceEdit(const QKeySequence &keySequence, QWidget *parent) : QKeySequenceEdit::QKeySequenceEdit(const QKeySequence &keySequence, QWidget *parent)
QWidget(*new QKeySequenceEditPrivate, parent, 0) : QKeySequenceEdit(parent)
{ {
Q_D(QKeySequenceEdit);
d->init();
setKeySequence(keySequence); setKeySequence(keySequence);
} }

View File

@ -233,10 +233,8 @@ QLabel::QLabel(QWidget *parent, Qt::WindowFlags f)
\sa setText(), setAlignment(), setFrameStyle(), setIndent() \sa setText(), setAlignment(), setFrameStyle(), setIndent()
*/ */
QLabel::QLabel(const QString &text, QWidget *parent, Qt::WindowFlags f) QLabel::QLabel(const QString &text, QWidget *parent, Qt::WindowFlags f)
: QFrame(*new QLabelPrivate(), parent, f) : QLabel(parent, f)
{ {
Q_D(QLabel);
d->init();
setText(text); setText(text);
} }

View File

@ -338,11 +338,8 @@ static const char *getSegments(char ch) // gets list of segments f
*/ */
QLCDNumber::QLCDNumber(QWidget *parent) QLCDNumber::QLCDNumber(QWidget *parent)
: QFrame(*new QLCDNumberPrivate, parent) : QLCDNumber(5, parent)
{ {
Q_D(QLCDNumber);
d->ndigits = 5;
d->init();
} }

View File

@ -260,10 +260,8 @@ void QLineEdit::initStyleOption(QStyleOptionFrame *option) const
\sa setText(), setMaxLength() \sa setText(), setMaxLength()
*/ */
QLineEdit::QLineEdit(QWidget* parent) QLineEdit::QLineEdit(QWidget* parent)
: QWidget(*new QLineEditPrivate, parent,0) : QLineEdit(QString(), parent)
{ {
Q_D(QLineEdit);
d->init(QString());
} }
/*! /*!

View File

@ -1481,10 +1481,9 @@ QMenu::QMenu(QWidget *parent)
\sa title \sa title
*/ */
QMenu::QMenu(const QString &title, QWidget *parent) QMenu::QMenu(const QString &title, QWidget *parent)
: QWidget(*new QMenuPrivate, parent, Qt::Popup) : QMenu(parent)
{ {
Q_D(QMenu); Q_D(QMenu);
d->init();
d->menuAction->setText(title); d->menuAction->setText(title);
} }

View File

@ -254,11 +254,9 @@ QPushButton::QPushButton(QWidget *parent)
*/ */
QPushButton::QPushButton(const QString &text, QWidget *parent) QPushButton::QPushButton(const QString &text, QWidget *parent)
: QAbstractButton(*new QPushButtonPrivate, parent) : QPushButton(parent)
{ {
Q_D(QPushButton);
setText(text); setText(text);
d->init();
} }
@ -270,12 +268,10 @@ QPushButton::QPushButton(const QString &text, QWidget *parent)
*/ */
QPushButton::QPushButton(const QIcon& icon, const QString &text, QWidget *parent) QPushButton::QPushButton(const QIcon& icon, const QString &text, QWidget *parent)
: QAbstractButton(*new QPushButtonPrivate, parent) : QPushButton(*new QPushButtonPrivate, parent)
{ {
Q_D(QPushButton);
setText(text); setText(text);
setIcon(icon); setIcon(icon);
d->init();
} }
/*! \internal /*! \internal

View File

@ -155,10 +155,8 @@ QRadioButton::~QRadioButton()
*/ */
QRadioButton::QRadioButton(const QString &text, QWidget *parent) QRadioButton::QRadioButton(const QString &text, QWidget *parent)
: QAbstractButton(*new QRadioButtonPrivate, parent) : QRadioButton(parent)
{ {
Q_D(QRadioButton);
d->init();
setText(text); setText(text);
} }

View File

@ -348,10 +348,8 @@ void QScrollBar::initStyleOption(QStyleOptionSlider *option) const
initial \l {QAbstractSlider::value} {value} of 0. initial \l {QAbstractSlider::value} {value} of 0.
*/ */
QScrollBar::QScrollBar(QWidget *parent) QScrollBar::QScrollBar(QWidget *parent)
: QAbstractSlider(*new QScrollBarPrivate, parent) : QScrollBar(Qt::Vertical, parent)
{ {
d_func()->orientation = Qt::Vertical;
d_func()->init();
} }
/*! /*!

View File

@ -284,10 +284,8 @@ QStyle::SubControl QSliderPrivate::newHoverControl(const QPoint &pos)
Constructs a vertical slider with the given \a parent. Constructs a vertical slider with the given \a parent.
*/ */
QSlider::QSlider(QWidget *parent) QSlider::QSlider(QWidget *parent)
: QAbstractSlider(*new QSliderPrivate, parent) : QSlider(Qt::Vertical, parent)
{ {
d_func()->orientation = Qt::Vertical;
d_func()->init();
} }
/*! /*!

View File

@ -941,11 +941,8 @@ QSplitterLayoutStruct *QSplitterPrivate::insertWidget(int index, QWidget *w)
\sa setOrientation() \sa setOrientation()
*/ */
QSplitter::QSplitter(QWidget *parent) QSplitter::QSplitter(QWidget *parent)
: QFrame(*new QSplitterPrivate, parent) : QSplitter(Qt::Horizontal, parent)
{ {
Q_D(QSplitter);
d->orient = Qt::Horizontal;
d->init();
} }

View File

@ -520,10 +520,8 @@ QToolBar::QToolBar(QWidget *parent)
\sa setWindowTitle() \sa setWindowTitle()
*/ */
QToolBar::QToolBar(const QString &title, QWidget *parent) QToolBar::QToolBar(const QString &title, QWidget *parent)
: QWidget(*new QToolBarPrivate, parent, 0) : QToolBar(parent)
{ {
Q_D(QToolBar);
d->init();
setWindowTitle(title); setWindowTitle(title);
} }