summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJ-P Nurmi <jpnurmi@digia.com>2013-08-27 14:50:59 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-09-03 18:51:13 +0200
commit53fa52bb604ab28ee37aba26e155486e94c66dec (patch)
tree82ebc7fb1893d99e870475253405ddd195b827c9
parentc304d741a27b5822a35d1fb83f8f5e65719907ce (diff)
downloadqtquickcontrols-53fa52bb604ab28ee37aba26e155486e94c66dec.tar.gz
TabView: fix dynamic tab handling
Task-number: QTBUG-33162 Change-Id: I873b37bd157230f80237fd40f3e5149fbd0207ca Reviewed-by: Volker Krause <volker.krause@kdab.com> Reviewed-by: Jens Bache-Wiig <jens.bache-wiig@digia.com>
-rw-r--r--src/controls/TabView.qml35
-rw-r--r--tests/auto/controls/data/tst_tabview.qml22
2 files changed, 45 insertions, 12 deletions
diff --git a/src/controls/TabView.qml b/src/controls/TabView.qml
index 6319d72c..cd8a023a 100644
--- a/src/controls/TabView.qml
+++ b/src/controls/TabView.qml
@@ -95,12 +95,14 @@ FocusScope {
Returns the newly added tab.
*/
function insertTab(index, title, component) {
- var tab = tabcomp.createObject(stack)
+ var tab = tabcomp.createObject()
tab.sourceComponent = component
- tab.parent = stack
tab.title = title
- tab.__inserted = true
+ // insert at appropriate index first, then set the parent to
+ // avoid onChildrenChanged appending it to the end of the list
__tabs.insert(index, {tab: tab})
+ tab.__inserted = true
+ tab.parent = stack
__setOpacities()
return tab
}
@@ -203,8 +205,17 @@ FocusScope {
property int frameWidth
property string style
+ property bool completed: false
- Component.onCompleted: addTabs(stack.children)
+ Component.onCompleted: {
+ addTabs(stack.children)
+ completed = true
+ }
+
+ onChildrenChanged: {
+ if (completed)
+ stack.addTabs(stack.children)
+ }
function addTabs(tabs) {
var tabAdded = false
@@ -212,12 +223,11 @@ FocusScope {
var tab = tabs[i]
if (!tab.__inserted && tab.Accessible.role === Accessible.LayeredPane) {
tab.__inserted = true
- if (tab.parent === root) {
- tab.parent = stack
- // a tab added dynamically by Component::createObject() and passing the
- // tab view as a parent should also get automatically removed when destructed
+ // reparent tabs created dynamically by createObject(tabView)
+ tab.parent = stack
+ // a dynamically added tab should also get automatically removed when destructed
+ if (completed)
tab.Component.onDestruction.connect(stack.onDynamicTabDestroyed.bind(tab))
- }
__tabs.append({tab: tab})
tabAdded = true
}
@@ -227,9 +237,10 @@ FocusScope {
}
function onDynamicTabDestroyed() {
- for (var i = 0; i < stack.children.length; ++i) {
- if (this === stack.children[i]) {
- root.removeTab(i)
+ for (var i = 0; i < __tabs.count; ++i) {
+ if (__tabs.get(i).tab === this) {
+ __tabs.remove(i, 1)
+ __setOpacities()
break
}
}
diff --git a/tests/auto/controls/data/tst_tabview.qml b/tests/auto/controls/data/tst_tabview.qml
index 8093ebc3..708f0383 100644
--- a/tests/auto/controls/data/tst_tabview.qml
+++ b/tests/auto/controls/data/tst_tabview.qml
@@ -223,6 +223,28 @@ TestCase {
tabView.destroy()
}
+ function test_dynamicModel() {
+ var test_tabView = ' \
+ import QtQuick 2.1; \
+ import QtQuick.Controls 1.0; \
+ TabView { \
+ id: tabView; \
+ property alias repeater: repeater; \
+ Repeater { id: repeater; Tab { } } \
+ } '
+
+ var tabView = Qt.createQmlObject(test_tabView, testCase, '')
+ compare(tabView.count, 0)
+
+ tabView.repeater.model = 4
+ compare(tabView.count, 4)
+
+ tabView.repeater.model = 0
+ compare(tabView.count, 0)
+
+ tabView.destroy()
+ }
+
function test_mousePressOnTabBar() {
var test_tabView = 'import QtQuick 2.1; \
import QtQuick.Controls 1.0; \