QDockAreaLayoutInfo::updateTabBar(): Save and restore current index.

When rebuilding the tab bar after hiding several dock widgets,
the index gets offset.

Task-number: QTBUG-49045
Change-Id: I05f6a976ca1d8c6f7cdf6532f1a728483398eabc
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
This commit is contained in:
Friedemann Kleint 2015-11-03 13:54:19 +01:00
parent ad0bc42d00
commit dbb013d984
2 changed files with 45 additions and 0 deletions

View File

@ -2105,6 +2105,8 @@ bool QDockAreaLayoutInfo::updateTabBar() const
const QSignalBlocker blocker(tabBar); const QSignalBlocker blocker(tabBar);
bool gap = false; bool gap = false;
const quintptr oldCurrentId = currentTabId();
int tab_idx = 0; int tab_idx = 0;
for (int i = 0; i < item_list.count(); ++i) { for (int i = 0; i < item_list.count(); ++i) {
const QDockAreaLayoutItem &item = item_list.at(i); const QDockAreaLayoutItem &item = item_list.at(i);
@ -2153,6 +2155,9 @@ bool QDockAreaLayoutInfo::updateTabBar() const
tabBar->removeTab(tab_idx); tabBar->removeTab(tab_idx);
} }
if (oldCurrentId > 0 && currentTabId() != oldCurrentId)
that->setCurrentTabId(oldCurrentId);
//returns if the tabbar is visible or not //returns if the tabbar is visible or not
return ( (gap ? 1 : 0) + tabBar->count()) > 1; return ( (gap ? 1 : 0) + tabBar->count()) > 1;
} }

View File

@ -40,6 +40,7 @@
#include <qdockwidget.h> #include <qdockwidget.h>
#include <qmainwindow.h> #include <qmainwindow.h>
#include <qlineedit.h> #include <qlineedit.h>
#include <qtabbar.h>
#include <QDesktopWidget> #include <QDesktopWidget>
#include <QtGui/QPainter> #include <QtGui/QPainter>
#include "private/qdockwidget_p.h" #include "private/qdockwidget_p.h"
@ -68,6 +69,7 @@ private slots:
void allowedAreas(); void allowedAreas();
void toggleViewAction(); void toggleViewAction();
void visibilityChanged(); void visibilityChanged();
void updateTabBarOnVisibilityChanged();
void dockLocationChanged(); void dockLocationChanged();
void setTitleBarWidget(); void setTitleBarWidget();
void titleBarDoubleClick(); void titleBarDoubleClick();
@ -586,6 +588,44 @@ void tst_QDockWidget::visibilityChanged()
QCOMPARE(spy.at(0).at(0).toBool(), true); QCOMPARE(spy.at(0).at(0).toBool(), true);
} }
void tst_QDockWidget::updateTabBarOnVisibilityChanged()
{
// QTBUG49045: Populate tabified dock area with 4 widgets, set the tab
// index to 2 (dw2), hide dw0, dw1 and check that the tab index is 0 (dw3).
QMainWindow mw;
mw.setMinimumSize(400, 400);
mw.setWindowTitle(QTest::currentTestFunction());
QDockWidget *dw0 = new QDockWidget("d1", &mw);
dw0->setAllowedAreas(Qt::LeftDockWidgetArea);
mw.addDockWidget(Qt::LeftDockWidgetArea, dw0);
QDockWidget *dw1 = new QDockWidget("d2", &mw);
dw1->setAllowedAreas(Qt::LeftDockWidgetArea);
mw.addDockWidget(Qt::LeftDockWidgetArea, dw1);
QDockWidget *dw2 = new QDockWidget("d3", &mw);
dw2->setAllowedAreas(Qt::LeftDockWidgetArea);
mw.addDockWidget(Qt::LeftDockWidgetArea, dw2);
QDockWidget *dw3 = new QDockWidget("d4", &mw);
dw3->setAllowedAreas(Qt::LeftDockWidgetArea);
mw.addDockWidget(Qt::LeftDockWidgetArea, dw3);
mw.tabifyDockWidget(dw0, dw1);
mw.tabifyDockWidget(dw1, dw2);
mw.tabifyDockWidget(dw2, dw3);
QTabBar *tabBar = mw.findChild<QTabBar *>();
QVERIFY(tabBar);
tabBar->setCurrentIndex(2);
mw.show();
QVERIFY(QTest::qWaitForWindowExposed(&mw));
QCOMPARE(tabBar->currentIndex(), 2);
dw0->hide();
dw1->hide();
QTRY_COMPARE(tabBar->count(), 2);
QCOMPARE(tabBar->currentIndex(), 0);
}
Q_DECLARE_METATYPE(Qt::DockWidgetArea) Q_DECLARE_METATYPE(Qt::DockWidgetArea)
void tst_QDockWidget::dockLocationChanged() void tst_QDockWidget::dockLocationChanged()