From b4ce31415baba84ca82aec68f4480b3e6ac89b90 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 29 Sep 2016 15:58:07 +0200 Subject: TabBar: fix mixing of fixed and implicitly resized tabs Don't calculate the implicit item width based on the total width of the whole control, but based on the available width that is left after subtracting the items that have an explicit width and won't be resized. Change-Id: Iae18dd9c9756b6f2afa143baab7d2501ce9d4697 Task-number: QTBUG-56265 Reviewed-by: Mitch Curtis --- src/quicktemplates2/qquicktabbar.cpp | 22 +++++++++++++++++----- tests/auto/controls/data/tst_tabbar.qml | 26 ++++++++++++++++++++------ 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/quicktemplates2/qquicktabbar.cpp b/src/quicktemplates2/qquicktabbar.cpp index 6fe734bb..bf6c4e74 100644 --- a/src/quicktemplates2/qquicktabbar.cpp +++ b/src/quicktemplates2/qquicktabbar.cpp @@ -113,16 +113,28 @@ void QQuickTabBarPrivate::updateLayout() Q_Q(QQuickTabBar); const int count = contentModel->count(); if (count > 0 && contentItem) { - const qreal itemWidth = (contentItem->width() - qMax(0, count - 1) * spacing) / count; + qreal reservedWidth = 0; + QVector resizableItems; + resizableItems.reserve(count); for (int i = 0; i < count; ++i) { QQuickItem *item = q->itemAt(i); if (item) { QQuickItemPrivate *p = QQuickItemPrivate::get(item); - if (!p->widthValid) { - item->setWidth(itemWidth); - p->widthValid = false; - } + if (!p->widthValid) + resizableItems += item; + else + reservedWidth += item->width(); + } + } + + if (!resizableItems.isEmpty()) { + const qreal totalSpacing = qMax(0, count - 1) * spacing; + const qreal itemWidth = (contentItem->width() - reservedWidth - totalSpacing) / resizableItems.count(); + + for (QQuickItem *item : qAsConst(resizableItems)) { + item->setWidth(itemWidth); + QQuickItemPrivate::get(item)->widthValid = false; } } } diff --git a/tests/auto/controls/data/tst_tabbar.qml b/tests/auto/controls/data/tst_tabbar.qml index b46f7f00..351db999 100644 --- a/tests/auto/controls/data/tst_tabbar.qml +++ b/tests/auto/controls/data/tst_tabbar.qml @@ -504,17 +504,31 @@ TestCase { control.destroy() } - function test_layout() { - var control = tabBar.createObject(testCase, {spacing: 0, width: 200}) + function test_layout_data() { + return [ + { tag: "spacing:0", spacing: 0 }, + { tag: "spacing:1", spacing: 1 }, + { tag: "spacing:10", spacing: 10 }, + ] + } + + function test_layout(data) { + var control = tabBar.createObject(testCase, {spacing: data.spacing, width: 200}) - var tab1 = tabButton.createObject(control) + var tab1 = tabButton.createObject(control, {text: "First"}) control.addItem(tab1) tryCompare(tab1, "width", control.width) - var tab2 = tabButton.createObject(control) + var tab2 = tabButton.createObject(control, {text: "Second"}) control.addItem(tab2) - tryCompare(tab1, "width", control.width / 2) - tryCompare(tab2, "width", control.width / 2) + tryCompare(tab1, "width", (control.width - data.spacing) / 2) + compare(tab2.width, (control.width - data.spacing) / 2) + + var tab3 = tabButton.createObject(control, {width: 50, text: "Third"}) + control.addItem(tab3) + tryCompare(tab1, "width", (control.width - 2 * data.spacing - 50) / 2) + compare(tab2.width, (control.width - 2 * data.spacing - 50) / 2) + compare(tab3.width, 50) control.destroy() } -- cgit v1.2.3