diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2017-02-02 23:19:04 +0100 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2017-04-28 07:37:43 +0000 |
commit | 7fe5e4c6858a74db582d95fae29a591b40ff7cdd (patch) | |
tree | 1b24766595edf6db80b7b75a74e2897b2bc65e76 /src/quicktemplates2/qquickbuttongroup.cpp | |
parent | ea470e8d612e5aa8c12eb3393baf057b8e8149e4 (diff) |
Add ButtonGroup::exclusive
[ChangeLog][Controls][ButtonGroup] Added exclusive property to allow
creating non-exclusive button groups.
Change-Id: Ic2f3f4bac852f03e5c25f9010993d62234a71e0b
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quicktemplates2/qquickbuttongroup.cpp')
-rw-r--r-- | src/quicktemplates2/qquickbuttongroup.cpp | 50 |
1 files changed, 46 insertions, 4 deletions
diff --git a/src/quicktemplates2/qquickbuttongroup.cpp b/src/quicktemplates2/qquickbuttongroup.cpp index a9c0d435..705224d6 100644 --- a/src/quicktemplates2/qquickbuttongroup.cpp +++ b/src/quicktemplates2/qquickbuttongroup.cpp @@ -154,7 +154,11 @@ class QQuickButtonGroupPrivate : public QObjectPrivate Q_DECLARE_PUBLIC(QQuickButtonGroup) public: - QQuickButtonGroupPrivate() : checkedButton(nullptr) { } + QQuickButtonGroupPrivate() + : exclusive(true), + checkedButton(nullptr) + { + } void clear(); void buttonClicked(); @@ -165,6 +169,7 @@ public: static QQuickAbstractButton *buttons_at(QQmlListProperty<QQuickAbstractButton> *prop, int index); static void buttons_clear(QQmlListProperty<QQuickAbstractButton> *prop); + bool exclusive; QQuickAbstractButton *checkedButton; QVector<QQuickAbstractButton*> buttons; }; @@ -190,6 +195,8 @@ void QQuickButtonGroupPrivate::buttonClicked() void QQuickButtonGroupPrivate::_q_updateCurrent() { Q_Q(QQuickButtonGroup); + if (!exclusive) + return; QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton*>(q->sender()); if (button && button->isChecked()) q->setCheckedButton(button); @@ -246,9 +253,12 @@ QQuickButtonGroupAttached *QQuickButtonGroup::qmlAttachedProperties(QObject *obj /*! \qmlproperty AbstractButton QtQuick.Controls::ButtonGroup::checkedButton - This property holds the currently selected button, or \c null if there is none. + This property holds the currently selected button in an exclusive group, + or \c null if there is none or the group is non-exclusive. + + By default, it is the first checked button added to an exclusive button group. - By default, it is the first checked button added to the button group. + \sa exclusive */ QQuickAbstractButton *QQuickButtonGroup::checkedButton() const { @@ -308,6 +318,38 @@ QQmlListProperty<QQuickAbstractButton> QQuickButtonGroup::buttons() } /*! + \since QtQuick.Controls 2.3 + \qmlproperty bool QtQuick.Controls::ButtonGroup::exclusive + + This property holds whether the button group is exclusive. The default value is \c true. + + If this property is \c true, then only one button in the group can be checked at any given time. + The user can click on any button to check it, and that button will replace the existing one as + the checked button in the group. + + In an exclusive group, the user cannot uncheck the currently checked button by clicking on it; + instead, another button in the group must be clicked to set the new checked button for that group. + + In a non-exclusive group, checking and unchecking buttons does not affect the other buttons in + the group. Furthermore, the value of the \l checkedButton property is \c null. +*/ +bool QQuickButtonGroup::isExclusive() const +{ + Q_D(const QQuickButtonGroup); + return d->exclusive; +} + +void QQuickButtonGroup::setExclusive(bool exclusive) +{ + Q_D(QQuickButtonGroup); + if (d->exclusive == exclusive) + return; + + d->exclusive = exclusive; + emit exclusiveChanged(); +} + +/*! \qmlmethod void QtQuick.Controls::ButtonGroup::addButton(AbstractButton button) Adds a \a button to the button group. @@ -328,7 +370,7 @@ void QQuickButtonGroup::addButton(QQuickAbstractButton *button) QObjectPrivate::connect(button, &QQuickAbstractButton::clicked, d, &QQuickButtonGroupPrivate::buttonClicked); QObjectPrivate::connect(button, &QQuickAbstractButton::checkedChanged, d, &QQuickButtonGroupPrivate::_q_updateCurrent); - if (button->isChecked()) + if (d->exclusive && button->isChecked()) setCheckedButton(button); d->buttons.append(button); |