summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMitch Curtis <mitch.curtis@qt.io>2016-12-06 09:31:22 +0100
committerMitch Curtis <mitch.curtis@qt.io>2016-12-06 11:24:05 +0000
commit1bcd068c082b3fab92637686447c2c82b4a0968c (patch)
tree218351c814a784cfa92a7ade55195574f7be4ebc
parent3bea717006ae660a933025514f21b7810262535a (diff)
StackView: don’t push duplicate items
It doesn’t make sense to do this. Task-number: QTBUG-57266 Change-Id: I23f740356f2727a59aa0a68cb57d2c44edfb6046 Reviewed-by: J-P Nurmi <jpnurmi@qt.io>
-rw-r--r--src/quicktemplates2/qquickstackview.cpp11
-rw-r--r--tests/auto/controls/data/tst_stackview.qml34
2 files changed, 42 insertions, 3 deletions
diff --git a/src/quicktemplates2/qquickstackview.cpp b/src/quicktemplates2/qquickstackview.cpp
index d16d7b16..07c471b5 100644
--- a/src/quicktemplates2/qquickstackview.cpp
+++ b/src/quicktemplates2/qquickstackview.cpp
@@ -464,6 +464,8 @@ QQuickItem *QQuickStackView::find(const QJSValue &callback, LoadBehavior behavio
\value StackView.ReplaceTransition An operation with replace transitions (since QtQuick.Controls 2.1).
\value StackView.PopTransition An operation with pop transitions (since QtQuick.Controls 2.1).
+ \note Items that already exist in the stack are not pushed.
+
\sa initialItem, {Pushing Items}
*/
void QQuickStackView::push(QQmlV4Function *args)
@@ -484,6 +486,15 @@ void QQuickStackView::push(QQmlV4Function *args)
operation = static_cast<Operation>(lastArg->toInt32());
QList<QQuickStackElement *> elements = d->parseElements(args);
+ // Remove any items that are already in the stack, as they can't be in two places at once.
+ for (int i = 0; i < elements.size(); ) {
+ QQuickStackElement *element = elements.at(i);
+ if (element->item && d->findElement(element->item))
+ elements.removeAt(i);
+ else
+ ++i;
+ }
+
if (elements.isEmpty()) {
qmlInfo(this) << "push: nothing to push";
args->setReturnValue(QV4::Encode::null());
diff --git a/tests/auto/controls/data/tst_stackview.qml b/tests/auto/controls/data/tst_stackview.qml
index b0e18389..44089e57 100644
--- a/tests/auto/controls/data/tst_stackview.qml
+++ b/tests/auto/controls/data/tst_stackview.qml
@@ -223,9 +223,9 @@ TestCase {
compare(control.depth, 0)
control.push(item, StackView.Immediate)
compare(control.depth, 1)
- control.push(item, StackView.Immediate)
- compare(control.depth, 2)
- control.pop(StackView.Immediate)
+ control.clear()
+ compare(control.depth, 0)
+ control.push(component, StackView.Immediate)
compare(control.depth, 1)
control.push(component, StackView.Immediate)
compare(control.depth, 2)
@@ -1020,4 +1020,32 @@ TestCase {
control.destroy()
}
+
+ function test_pushSameItem() {
+ var control = stackView.createObject(testCase)
+ verify(control)
+
+ control.push(item, StackView.Immediate)
+ compare(control.currentItem, item)
+ compare(control.depth, 1)
+
+ // Pushing the same Item should do nothing.
+ ignoreWarning(Qt.resolvedUrl("tst_stackview.qml") + ":59:9: QML StackView: push: nothing to push")
+ control.push(item, StackView.Immediate)
+ compare(control.currentItem, item)
+ compare(control.depth, 1)
+
+ // Push a component so that it becomes current.
+ var current = control.push(component, StackView.Immediate)
+ compare(control.currentItem, current)
+ compare(control.depth, 2)
+
+ // Push a bunch of stuff. "item" is already in the stack, so it should be ignored.
+ current = control.push(component, item, StackView.Immediate)
+ verify(current !== item)
+ compare(control.currentItem, current)
+ compare(control.depth, 3)
+
+ control.destroy()
+ }
}