aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/imports/templates/qtquicktemplates2plugin.cpp1
-rw-r--r--src/quicktemplates2/qquickcheckdelegate.cpp50
-rw-r--r--src/quicktemplates2/qquickcheckdelegate_p.h4
-rw-r--r--tests/auto/controls/data/tst_checkdelegate.qml47
4 files changed, 101 insertions, 1 deletions
diff --git a/src/imports/templates/qtquicktemplates2plugin.cpp b/src/imports/templates/qtquicktemplates2plugin.cpp
index 3564f957..a491c031 100644
--- a/src/imports/templates/qtquicktemplates2plugin.cpp
+++ b/src/imports/templates/qtquicktemplates2plugin.cpp
@@ -323,6 +323,7 @@ void QtQuickTemplates2Plugin::registerTypes(const char *uri)
qmlRegisterType<QQuickAbstractButton, 4>(uri, 2, 4, "AbstractButton");
qmlRegisterType<QQuickButtonGroup, 4>(uri, 2, 4, "ButtonGroup");
qmlRegisterType<QQuickCheckBox, 4>(uri, 2, 4, "CheckBox");
+ qmlRegisterType<QQuickCheckDelegate, 4>(uri, 2, 4, "CheckDelegate");
qmlRegisterType<QQuickScrollBar, 4>(uri, 2, 4, "ScrollBar");
qmlRegisterType<QQuickScrollIndicator, 4>(uri, 2, 4, "ScrollIndicator");
qmlRegisterType<QQuickSpinBox, 4>(uri, 2, 4, "SpinBox");
diff --git a/src/quicktemplates2/qquickcheckdelegate.cpp b/src/quicktemplates2/qquickcheckdelegate.cpp
index 78d55e1d..9fd8c183 100644
--- a/src/quicktemplates2/qquickcheckdelegate.cpp
+++ b/src/quicktemplates2/qquickcheckdelegate.cpp
@@ -38,6 +38,7 @@
#include "qquickitemdelegate_p_p.h"
#include <QtGui/qpa/qplatformtheme.h>
+#include <QtQml/qjsvalue.h>
QT_BEGIN_NAMESPACE
@@ -94,10 +95,20 @@ public:
{
}
+ void setNextCheckState(const QJSValue &callback);
+
bool tristate;
Qt::CheckState checkState;
+ QJSValue nextCheckState;
};
+void QQuickCheckDelegatePrivate::setNextCheckState(const QJSValue &callback)
+{
+ Q_Q(QQuickCheckDelegate);
+ nextCheckState = callback;
+ emit q->nextCheckStateChanged();
+}
+
QQuickCheckDelegate::QQuickCheckDelegate(QQuickItem *parent)
: QQuickItemDelegate(*(new QQuickCheckDelegatePrivate), parent)
{
@@ -179,10 +190,45 @@ void QQuickCheckDelegate::buttonChange(ButtonChange change)
QQuickAbstractButton::buttonChange(change);
}
+/*!
+ \since QtQuick.Controls 2.4 (Qt 5.11)
+ \qmlproperty function QtQuick.Controls::CheckDelegate::nextCheckState
+
+ This property holds a callback function that is called to determine
+ the next check state whenever the check delegate is interactively toggled
+ by the user via touch, mouse, or keyboard.
+
+ By default, a normal check delegate cycles between \c Qt.Unchecked and
+ \c Qt.Checked states, and a tri-state check delegate cycles between
+ \c Qt.Unchecked, \c Qt.PartiallyChecked, and \c Qt.Checked states.
+
+ The \c nextCheckState callback function can override the default behavior.
+ The following example implements a tri-state check delegate that can present
+ a partially checked state depending on external conditions, but never
+ cycles to the partially checked state when interactively toggled by
+ the user.
+
+ \code
+ CheckDelegate {
+ tristate: true
+ checkState: allChildrenChecked ? Qt.Checked :
+ anyChildChecked ? Qt.PartiallyChecked : Qt.Unchecked
+
+ nextCheckState: function() {
+ if (checkState === Qt.Checked)
+ return Qt.Unchecked
+ else
+ return Qt.Checked
+ }
+ }
+ \endcode
+*/
void QQuickCheckDelegate::nextCheckState()
{
Q_D(QQuickCheckDelegate);
- if (d->tristate)
+ if (d->nextCheckState.isCallable())
+ setCheckState(static_cast<Qt::CheckState>(d->nextCheckState.call().toInt()));
+ else if (d->tristate)
setCheckState(static_cast<Qt::CheckState>((d->checkState + 1) % 3));
else
QQuickItemDelegate::nextCheckState();
@@ -196,3 +242,5 @@ QAccessible::Role QQuickCheckDelegate::accessibleRole() const
#endif
QT_END_NAMESPACE
+
+#include "moc_qquickcheckdelegate_p.cpp"
diff --git a/src/quicktemplates2/qquickcheckdelegate_p.h b/src/quicktemplates2/qquickcheckdelegate_p.h
index ae2b4f52..67dcf706 100644
--- a/src/quicktemplates2/qquickcheckdelegate_p.h
+++ b/src/quicktemplates2/qquickcheckdelegate_p.h
@@ -59,6 +59,8 @@ class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickCheckDelegate : public QQuickItemDe
Q_OBJECT
Q_PROPERTY(bool tristate READ isTristate WRITE setTristate NOTIFY tristateChanged FINAL)
Q_PROPERTY(Qt::CheckState checkState READ checkState WRITE setCheckState NOTIFY checkStateChanged FINAL)
+ // 2.4 (Qt 5.11)
+ Q_PRIVATE_PROPERTY(QQuickCheckDelegate::d_func(), QJSValue nextCheckState MEMBER nextCheckState WRITE setNextCheckState NOTIFY nextCheckStateChanged FINAL REVISION 4)
public:
explicit QQuickCheckDelegate(QQuickItem *parent = nullptr);
@@ -72,6 +74,8 @@ public:
Q_SIGNALS:
void tristateChanged();
void checkStateChanged();
+ // 2.4 (Qt 5.11)
+ Q_REVISION(4) void nextCheckStateChanged();
protected:
QFont defaultFont() const override;
diff --git a/tests/auto/controls/data/tst_checkdelegate.qml b/tests/auto/controls/data/tst_checkdelegate.qml
index 9f92b4dc..72b45251 100644
--- a/tests/auto/controls/data/tst_checkdelegate.qml
+++ b/tests/auto/controls/data/tst_checkdelegate.qml
@@ -170,4 +170,51 @@ TestCase {
break;
}
}
+
+ Component {
+ id: nextCheckStateDelegate
+ CheckDelegate {
+ tristate: true
+ nextCheckState: function() {
+ if (checkState === Qt.Checked)
+ return Qt.Unchecked
+ else
+ return Qt.Checked
+ }
+ }
+ }
+
+ function test_nextCheckState_data() {
+ return [
+ { tag: "unchecked", checkState: Qt.Unchecked, expectedState: Qt.Checked },
+ { tag: "partially-checked", checkState: Qt.PartiallyChecked, expectedState: Qt.Checked },
+ { tag: "checked", checkState: Qt.Checked, expectedState: Qt.Unchecked }
+ ]
+ }
+
+ function test_nextCheckState(data) {
+ var control = createTemporaryObject(nextCheckStateDelegate, testCase)
+ verify(control)
+
+ // mouse
+ control.checkState = data.checkState
+ compare(control.checkState, data.checkState)
+ mouseClick(control)
+ compare(control.checkState, data.expectedState)
+
+ // touch
+ control.checkState = data.checkState
+ compare(control.checkState, data.checkState)
+ var touch = touchEvent(control)
+ touch.press(0, control).commit().release(0, control).commit()
+ compare(control.checkState, data.expectedState)
+
+ // keyboard
+ control.forceActiveFocus()
+ tryCompare(control, "activeFocus", true)
+ control.checkState = data.checkState
+ compare(control.checkState, data.checkState)
+ keyClick(Qt.Key_Space)
+ compare(control.checkState, data.expectedState)
+ }
}