aboutsummaryrefslogtreecommitdiffstats
path: root/src/controls
diff options
context:
space:
mode:
authorJ-P Nurmi <jpnurmi@theqtcompany.com>2015-09-08 10:13:34 +0200
committerJ-P Nurmi <jpnurmi@theqtcompany.com>2015-09-10 15:25:25 +0000
commit34dcf20a0536ec7df4f847b16f03e6f66f308025 (patch)
tree4787089abdfaea7599180c4d7ef68e746d047c98 /src/controls
parent8a0135b82e9b354fe5304144a12c850c248f2a79 (diff)
Add ExclusiveGroup::checkables
Exclusive checkables typically share the same parent item. For example, a row or column positioner or layout. This change makes it convenient to handle such cases so that one can add the whole list of children to an exclusive group, without the need of creating any attached objects. Column { id: column ExclusiveGroup { id: eg checkables: column.children } Switch { id: s1; text: qsTr("First") } Switch { id: s2; text: qsTr("Second") } Switch { id: s3; text: qsTr("Third") } } Last but not least, this allows breaking the C++ dependency between QQuickTabBar and QQuickExclusiveGroup. Change-Id: Ic75065b87ef8fa532f77118d740f04484994bbf0 Reviewed-by: Gabriel de Dietrich <gabriel.dedietrich@theqtcompany.com>
Diffstat (limited to 'src/controls')
-rw-r--r--src/controls/qquickexclusivegroup.cpp143
-rw-r--r--src/controls/qquickexclusivegroup_p.h5
2 files changed, 138 insertions, 10 deletions
diff --git a/src/controls/qquickexclusivegroup.cpp b/src/controls/qquickexclusivegroup.cpp
index 1531b11e..20a863cc 100644
--- a/src/controls/qquickexclusivegroup.cpp
+++ b/src/controls/qquickexclusivegroup.cpp
@@ -51,15 +51,53 @@ QT_BEGIN_NAMESPACE
\brief An exclusive group of checkable controls.
ExclusiveGroup is a non-visual, mutually exclusive group of checkable
- controls and objects. It is used with controls such as RadioButton, where only one of the options can be selected at a time.
+ controls and objects. It is used with controls such as RadioButton,
+ where only one of the options can be selected at a time.
Any control or object that has a \c checked property, and either a
\c checkedChanged(), \c toggled(), or \c toggled(bool) signal, can be
- attached to an ExclusiveGroup.
+ added to an ExclusiveGroup.
+
+ The most straight-forward way to use ExclusiveGroup is to assign
+ a list of checkable items. For example, the list of children of a
+ \l{Item Positioners}{positioner} or a \l{Qt Quick Layouts}{layout}
+ that manages a group of mutually exclusive controls.
\code
+ ExclusiveGroup {
+ checkables: column.children
+ }
+
Column {
- ExclusiveGroup { id: radioGroup }
+ id: column
+
+ RadioButton {
+ checked: true
+ text: qsTr("DAB")
+ }
+
+ RadioButton {
+ text: qsTr("FM")
+ }
+
+ RadioButton {
+ text: qsTr("AM")
+ }
+ }
+ \endcode
+
+ Mutually exclusive controls do not always share the same parent item,
+ or the parent layout may sometimes contain items that should not be
+ included to the exclusive group. Such cases are best handled using
+ the \l group attached property.
+
+ \code
+ ExclusiveGroup { id: radioGroup }
+
+ Column {
+ Label {
+ text: qsTr("Radio:")
+ }
RadioButton {
checked: true
@@ -79,6 +117,9 @@ QT_BEGIN_NAMESPACE
}
\endcode
+ More advanced use cases can be handled using the addCheckable() and
+ removeCheckable() methods.
+
\sa RadioButton
*/
@@ -119,7 +160,13 @@ public:
void _q_updateCurrent();
+ static void checkables_append(QQmlListProperty<QObject> *prop, QObject *obj);
+ static int checkables_count(QQmlListProperty<QObject> *prop);
+ static QObject *checkables_at(QQmlListProperty<QObject> *prop, int index);
+ static void checkables_clear(QQmlListProperty<QObject> *prop);
+
QObject *current;
+ QObjectList checkables;
QMetaMethod updateCurrentMethod;
};
@@ -131,6 +178,35 @@ void QQuickExclusiveGroupPrivate::_q_updateCurrent()
q->setCurrent(object);
}
+void QQuickExclusiveGroupPrivate::checkables_append(QQmlListProperty<QObject> *prop, QObject *obj)
+{
+ QQuickExclusiveGroup *q = static_cast<QQuickExclusiveGroup *>(prop->object);
+ q->addCheckable(obj);
+}
+
+int QQuickExclusiveGroupPrivate::checkables_count(QQmlListProperty<QObject> *prop)
+{
+ QQuickExclusiveGroupPrivate *p = static_cast<QQuickExclusiveGroupPrivate *>(prop->data);
+ return p->checkables.count();
+}
+
+QObject *QQuickExclusiveGroupPrivate::checkables_at(QQmlListProperty<QObject> *prop, int index)
+{
+ QQuickExclusiveGroupPrivate *p = static_cast<QQuickExclusiveGroupPrivate *>(prop->data);
+ return p->checkables.value(index);
+}
+
+void QQuickExclusiveGroupPrivate::checkables_clear(QQmlListProperty<QObject> *prop)
+{
+ QQuickExclusiveGroupPrivate *p = static_cast<QQuickExclusiveGroupPrivate *>(prop->data);
+ if (!p->checkables.isEmpty()) {
+ p->checkables.clear();
+ QQuickExclusiveGroup *q = static_cast<QQuickExclusiveGroup *>(prop->object);
+ q->setCurrent(0);
+ emit q->checkablesChanged();
+ }
+}
+
QQuickExclusiveGroup::QQuickExclusiveGroup(QObject *parent)
: QObject(*(new QQuickExclusiveGroupPrivate), parent)
{
@@ -149,7 +225,7 @@ QQuickExclusiveGroupAttached *QQuickExclusiveGroup::qmlAttachedProperties(QObjec
This property holds the currently selected object or \c null if there is none.
- By default, it is the first checked object attached to the exclusive group.
+ By default, it is the first checked object added to the exclusive group.
*/
QObject *QQuickExclusiveGroup::current() const
{
@@ -171,19 +247,57 @@ void QQuickExclusiveGroup::setCurrent(QObject *current)
}
/*!
+ \qmlproperty list<Object> QtQuickControls2::ExclusiveGroup::checkables
+ \default
+
+ This property holds the list of checkables.
+
+ \code
+ ExclusiveGroup {
+ checkables: column.children
+ }
+
+ Column {
+ id: column
+
+ RadioButton {
+ checked: true
+ text: qsTr("Option A")
+ }
+
+ RadioButton {
+ text: qsTr("Option B")
+ }
+ }
+ \endcode
+
+ \sa group
+*/
+QQmlListProperty<QObject> QQuickExclusiveGroup::checkables()
+{
+ Q_D(QQuickExclusiveGroup);
+ return QQmlListProperty<QObject>(this, d,
+ QQuickExclusiveGroupPrivate::checkables_append,
+ QQuickExclusiveGroupPrivate::checkables_count,
+ QQuickExclusiveGroupPrivate::checkables_at,
+ QQuickExclusiveGroupPrivate::checkables_clear);
+}
+
+/*!
\qmlmethod void QtQuickControls2::ExclusiveGroup::addCheckable(QtObject object)
Adds an \a object to the exclusive group.
\note Manually adding objects to an exclusive group is typically unnecessary.
- The \l group attached property provides a convenient and declarative syntax.
+ The \l checkables property and the \l group attached property provide a
+ convenient and declarative syntax.
- \sa group
+ \sa checkables, group
*/
void QQuickExclusiveGroup::addCheckable(QObject *object)
{
Q_D(QQuickExclusiveGroup);
- if (!object)
+ if (!object || d->checkables.contains(object))
return;
QMetaMethod signal = checkableSignal(object);
@@ -193,6 +307,9 @@ void QQuickExclusiveGroup::addCheckable(QObject *object)
if (isChecked(object))
setCurrent(object);
+
+ d->checkables.append(object);
+ emit checkablesChanged();
} else {
qmlInfo(this) << "The object has no checkedChanged() or toggled() signal.";
}
@@ -204,14 +321,15 @@ void QQuickExclusiveGroup::addCheckable(QObject *object)
Removes an \a object from the exclusive group.
\note Manually removing objects from an exclusive group is typically unnecessary.
- The \l group attached property provides a convenient and declarative syntax.
+ The \l checkables property and the \l group attached property provide a
+ convenient and declarative syntax.
- \sa group
+ \sa checkables, group
*/
void QQuickExclusiveGroup::removeCheckable(QObject *object)
{
Q_D(QQuickExclusiveGroup);
- if (!object)
+ if (!object || !d->checkables.contains(object))
return;
QMetaMethod signal = checkableSignal(object);
@@ -222,6 +340,9 @@ void QQuickExclusiveGroup::removeCheckable(QObject *object)
if (d->current == object)
setCurrent(Q_NULLPTR);
+
+ d->checkables.removeOne(object);
+ emit checkablesChanged();
}
class QQuickExclusiveGroupAttachedPrivate : public QObjectPrivate
@@ -256,6 +377,8 @@ QQuickExclusiveGroupAttached::QQuickExclusiveGroupAttached(QObject *parent) :
ExclusiveGroup.group: group
}
\endcode
+
+ \sa checkables
*/
QQuickExclusiveGroup *QQuickExclusiveGroupAttached::group() const
{
diff --git a/src/controls/qquickexclusivegroup_p.h b/src/controls/qquickexclusivegroup_p.h
index 434425d0..0c4962aa 100644
--- a/src/controls/qquickexclusivegroup_p.h
+++ b/src/controls/qquickexclusivegroup_p.h
@@ -62,6 +62,8 @@ class Q_QUICKCONTROLS_EXPORT QQuickExclusiveGroup : public QObject
{
Q_OBJECT
Q_PROPERTY(QObject *current READ current WRITE setCurrent NOTIFY currentChanged)
+ Q_PROPERTY(QQmlListProperty<QObject> checkables READ checkables NOTIFY checkablesChanged FINAL)
+ Q_CLASSINFO("DefaultProperty", "checkables")
public:
explicit QQuickExclusiveGroup(QObject *parent = Q_NULLPTR);
@@ -71,12 +73,15 @@ public:
QObject *current() const;
void setCurrent(QObject *current);
+ QQmlListProperty<QObject> checkables();
+
public Q_SLOTS:
void addCheckable(QObject *object);
void removeCheckable(QObject *object);
Q_SIGNALS:
void currentChanged();
+ void checkablesChanged();
private:
Q_DISABLE_COPY(QQuickExclusiveGroup)