diff options
author | J-P Nurmi <jpnurmi@theqtcompany.com> | 2015-04-24 19:12:20 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@theqtcompany.com> | 2015-04-28 08:24:47 +0000 |
commit | 5df44d55b5230a39f512e36e308569f28c80264b (patch) | |
tree | 3fe974e29dd3641ed3580ac9f6ab912932478480 /src/controls/qquickbutton.cpp | |
parent | 1590f21ad105b239f129d52b5eebd14f23b926bb (diff) |
Button: provide a default size for the label delegate
If the label delegate has no explicit size defined, make it follow the
size of the button minus padding. This should be a convenient default
behavior that satisfies the most users, and saves a few of QML bindings.
Makes also the upcoming "the default delegate implementation looks like
this" -snippet nice and short. ;)
Change-Id: I574439c1e52c2904746be7d830f9536d4cfd9083
Reviewed-by: Frederik Gladhorn <frederik.gladhorn@theqtcompany.com>
Diffstat (limited to 'src/controls/qquickbutton.cpp')
-rw-r--r-- | src/controls/qquickbutton.cpp | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/src/controls/qquickbutton.cpp b/src/controls/qquickbutton.cpp index 64d7057e..65d16494 100644 --- a/src/controls/qquickbutton.cpp +++ b/src/controls/qquickbutton.cpp @@ -69,10 +69,25 @@ QT_BEGIN_NAMESPACE */ QQuickButtonPrivate::QQuickButtonPrivate() : - pressed(false), label(Q_NULLPTR) + pressed(false), label(Q_NULLPTR), labelHasWidth(true), labelHasHeight(true) { } +void QQuickButtonPrivate::updateGeometry() +{ + Q_Q(QQuickButton); + if (label) { + if (!labelHasWidth) { + label->setX(q->leftPadding()); + label->setWidth(q->width() - q->leftPadding() - q->rightPadding()); + } + if (!labelHasHeight) { + label->setY(q->topPadding()); + label->setHeight(q->height() - q->topPadding() - q->bottomPadding()); + } + } +} + QQuickButton::QQuickButton(QQuickItem *parent) : QQuickControl(*(new QQuickButtonPrivate), parent) { @@ -208,4 +223,27 @@ void QQuickButton::mouseUngrabEvent() setPressed(false); } +void QQuickButton::componentComplete() +{ + Q_D(QQuickButton); + QQuickControl::componentComplete(); + d->labelHasWidth = d->label && QQuickItemPrivate::get(d->label)->widthValid; + d->labelHasHeight = d->label && QQuickItemPrivate::get(d->label)->heightValid; + d->updateGeometry(); +} + +void QQuickButton::paddingChange() +{ + Q_D(QQuickButton); + QQuickControl::paddingChange(); + d->updateGeometry(); +} + +void QQuickButton::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) +{ + Q_D(QQuickButton); + QQuickControl::geometryChanged(newGeometry, oldGeometry); + d->updateGeometry(); +} + QT_END_NAMESPACE |