From c6c039167cfdbc28351a73cc0819461c69bc0451 Mon Sep 17 00:00:00 2001 From: Zhang Yu Date: Tue, 30 Nov 2021 11:14:05 +0800 Subject: [PATCH] Fix fail to activate first sub window with QMdiArea::TabbedView The first sub window added will activate itself automatically, and isActive is set to true. Therefore the call to setActiveSubWindow to activate the first sub window will be ignored. When showing the mdiarea, all sub windows will be activated in the order in which they were added, so the active window will always be the last sub window added. Fix this by setting isActive to false so that setActiveSubWindow activates the first sub window when the mdiarea becomes active. Fixes: QTBUG-92037 Pick-to: 6.2 Change-Id: Id4a793e2059803c1a4ada916fdae2d3cc02cdf06 Reviewed-by: Qt CI Bot Reviewed-by: Volker Hilsheimer --- src/widgets/widgets/qmdiarea.cpp | 4 ++ .../widgets/widgets/qmdiarea/tst_qmdiarea.cpp | 57 +++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/widgets/widgets/qmdiarea.cpp b/src/widgets/widgets/qmdiarea.cpp index b5671180a2..3ab8632f80 100644 --- a/src/widgets/widgets/qmdiarea.cpp +++ b/src/widgets/widgets/qmdiarea.cpp @@ -1000,6 +1000,10 @@ void QMdiAreaPrivate::activateWindow(QMdiSubWindow *child) if (child->isHidden() || child == active) return; + + if (child->d_func()->isActive && active == nullptr) + child->d_func()->isActive = false; + child->d_func()->setActive(true); } diff --git a/tests/auto/widgets/widgets/qmdiarea/tst_qmdiarea.cpp b/tests/auto/widgets/widgets/qmdiarea/tst_qmdiarea.cpp index 225b349f08..55ac17ac70 100644 --- a/tests/auto/widgets/widgets/qmdiarea/tst_qmdiarea.cpp +++ b/tests/auto/widgets/widgets/qmdiarea/tst_qmdiarea.cpp @@ -283,6 +283,9 @@ private slots: void task_236750(); void qtbug92240_title_data(); void qtbug92240_title(); + void tabbedview_activefirst(); + void tabbedview_activesecond(); + void tabbedview_activethird(); private: QMdiSubWindow *activeWindow; @@ -2738,6 +2741,60 @@ void tst_QMdiArea::qtbug92240_title() QTRY_COMPARE(w.windowTitle(), QLatin1String("QTBUG-92240 - [2]")); } +static void setupMdiAreaWithTabbedView(QMdiArea &mdiArea) +{ + mdiArea.setViewMode(QMdiArea::TabbedView); + + auto *mdiWin1 = new QWidget(&mdiArea); + mdiWin1->setWindowTitle(QLatin1String("Sub1")); + mdiArea.addSubWindow(mdiWin1); + + auto *mdiWin2 = new QWidget(&mdiArea); + mdiWin2->setWindowTitle(QLatin1String("Sub2")); + mdiArea.addSubWindow(mdiWin2); + + auto *mdiWin3 = new QWidget(&mdiArea); + mdiWin3->setWindowTitle(QLatin1String("Sub3")); + mdiArea.addSubWindow(mdiWin3); +} + +void tst_QMdiArea::tabbedview_activefirst() +{ + QMdiArea mdiArea; + setupMdiAreaWithTabbedView(mdiArea); + + auto sub0 = mdiArea.subWindowList().at(0); + mdiArea.setActiveSubWindow(sub0); + mdiArea.show(); + QVERIFY(QTest::qWaitForWindowExposed(&mdiArea)); + QCOMPARE(mdiArea.activeSubWindow(), sub0); +} + +void tst_QMdiArea::tabbedview_activesecond() +{ + QMdiArea mdiArea; + setupMdiAreaWithTabbedView(mdiArea); + + auto sub1 = mdiArea.subWindowList().at(1); + mdiArea.setActiveSubWindow(sub1); + mdiArea.show(); + QVERIFY(QTest::qWaitForWindowExposed(&mdiArea)); + QCOMPARE(mdiArea.activeSubWindow(), sub1); +} + +void tst_QMdiArea::tabbedview_activethird() +{ + QMdiArea mdiArea; + setupMdiAreaWithTabbedView(mdiArea); + + auto sub2 = mdiArea.subWindowList().at(2); + mdiArea.setActiveSubWindow(sub2); + mdiArea.show(); + QVERIFY(QTest::qWaitForWindowExposed(&mdiArea)); + QCOMPARE(mdiArea.activeSubWindow(), sub2); +} + + QTEST_MAIN(tst_QMdiArea) #include "tst_qmdiarea.moc"