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 <qt_ci_bot@qt-project.org>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Zhang Yu 2021-11-30 11:14:05 +08:00
parent f814241abe
commit c6c039167c
2 changed files with 61 additions and 0 deletions

View File

@ -1000,6 +1000,10 @@ void QMdiAreaPrivate::activateWindow(QMdiSubWindow *child)
if (child->isHidden() || child == active) if (child->isHidden() || child == active)
return; return;
if (child->d_func()->isActive && active == nullptr)
child->d_func()->isActive = false;
child->d_func()->setActive(true); child->d_func()->setActive(true);
} }

View File

@ -283,6 +283,9 @@ private slots:
void task_236750(); void task_236750();
void qtbug92240_title_data(); void qtbug92240_title_data();
void qtbug92240_title(); void qtbug92240_title();
void tabbedview_activefirst();
void tabbedview_activesecond();
void tabbedview_activethird();
private: private:
QMdiSubWindow *activeWindow; QMdiSubWindow *activeWindow;
@ -2738,6 +2741,60 @@ void tst_QMdiArea::qtbug92240_title()
QTRY_COMPARE(w.windowTitle(), QLatin1String("QTBUG-92240 - [2]")); 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) QTEST_MAIN(tst_QMdiArea)
#include "tst_qmdiarea.moc" #include "tst_qmdiarea.moc"