diff --git a/src/widgets/widgets/qmdisubwindow.cpp b/src/widgets/widgets/qmdisubwindow.cpp index 89d77574f0..1c03b02ab8 100644 --- a/src/widgets/widgets/qmdisubwindow.cpp +++ b/src/widgets/widgets/qmdisubwindow.cpp @@ -263,11 +263,28 @@ static inline ControlElement *ptr(QWidget *widget) return nullptr; } +QString QMdiSubWindowPrivate::originalWindowTitleHelper() const +{ + Q_Q(const QMdiSubWindow); + // QTBUG-92240: When DontMaximizeSubWindowOnActivation is set and + // there is another subwindow maximized, use its original title. + if (auto *mdiArea = q->mdiArea()) { + const auto &subWindows = mdiArea->subWindowList(); + for (auto *subWindow : subWindows) { + if (subWindow != q && subWindow->isMaximized()) { + auto *subWindowD = static_cast(qt_widget_private(subWindow)); + if (!subWindowD->originalTitle.isNull()) + return subWindowD->originalTitle; + } + } + } + return q->window()->windowTitle(); +} + QString QMdiSubWindowPrivate::originalWindowTitle() { - Q_Q(QMdiSubWindow); if (originalTitle.isNull()) { - originalTitle = q->window()->windowTitle(); + originalTitle = originalWindowTitleHelper(); if (originalTitle.isNull()) originalTitle = QLatin1String(""); } @@ -282,11 +299,17 @@ void QMdiSubWindowPrivate::setNewWindowTitle() return; QString original = originalWindowTitle(); if (!original.isEmpty()) { - if (!original.contains(QMdiSubWindow::tr("- [%1]").arg(childTitle))) - q->window()->setWindowTitle(QMdiSubWindow::tr("%1 - [%2]").arg(original, childTitle)); + if (!original.contains(QMdiSubWindow::tr("- [%1]").arg(childTitle))) { + auto title = QMdiSubWindow::tr("%1 - [%2]").arg(original, childTitle); + ignoreWindowTitleChange = true; + q->window()->setWindowTitle(title); + ignoreWindowTitleChange = false; + } } else { + ignoreWindowTitleChange = true; q->window()->setWindowTitle(childTitle); + ignoreWindowTitleChange = false; } } diff --git a/src/widgets/widgets/qmdisubwindow_p.h b/src/widgets/widgets/qmdisubwindow_p.h index d3513b6708..043cf92201 100644 --- a/src/widgets/widgets/qmdisubwindow_p.h +++ b/src/widgets/widgets/qmdisubwindow_p.h @@ -286,6 +286,7 @@ public: #endif void updateInternalWindowTitle(); QString originalWindowTitle(); + QString originalWindowTitleHelper() const; void setNewWindowTitle(); inline int titleBarHeight() const diff --git a/tests/auto/widgets/widgets/qmdiarea/tst_qmdiarea.cpp b/tests/auto/widgets/widgets/qmdiarea/tst_qmdiarea.cpp index 6df7c74bdc..225b349f08 100644 --- a/tests/auto/widgets/widgets/qmdiarea/tst_qmdiarea.cpp +++ b/tests/auto/widgets/widgets/qmdiarea/tst_qmdiarea.cpp @@ -1,4 +1,4 @@ -/**************************************************************************** +/**************************************************************************** ** ** Copyright (C) 2016 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ @@ -281,6 +281,8 @@ private slots: void nativeSubWindows(); void task_209615(); void task_236750(); + void qtbug92240_title_data(); + void qtbug92240_title(); private: QMdiSubWindow *activeWindow; @@ -2697,6 +2699,45 @@ void tst_QMdiArea::task_236750() subWindow->showMinimized(); } +// QTBUG-92240: When subwindows are maximized, their title is supposed to +// appear on the main window. When DontMaximizeSubWindowOnActivation was set, +// titles of previously created maximized windows interfered, resulting in +// "QTBUG-92240 - [1] - [2]". +void tst_QMdiArea::qtbug92240_title_data() +{ + QTest::addColumn("dontMaximize"); + QTest::newRow("default") << false; + QTest::newRow("dontMaximize") << true; +} + +void tst_QMdiArea::qtbug92240_title() +{ + QFETCH(bool, dontMaximize); + +#ifdef Q_OS_MACOS + QSKIP("Not supported on macOS"); +#endif + + QMainWindow w; + const QString title = QStringLiteral("QTBUG-92240"); + w.setWindowTitle(title); + w.menuBar()->addMenu(QStringLiteral("File")); + w.show(); + + auto *mdiArea = new QMdiArea; + w.setCentralWidget(mdiArea); + if (dontMaximize) + mdiArea->setOption(QMdiArea::DontMaximizeSubWindowOnActivation); + auto *sw1 = mdiArea->addSubWindow(new QWidget); + sw1->setWindowTitle(QStringLiteral("1")); + sw1->showMaximized(); + QTRY_COMPARE(w.windowTitle(), QLatin1String("QTBUG-92240 - [1]")); + auto *sw2 = mdiArea->addSubWindow(new QWidget); + sw2->setWindowTitle(QStringLiteral("2")); + sw2->showMaximized(); + QTRY_COMPARE(w.windowTitle(), QLatin1String("QTBUG-92240 - [2]")); +} + QTEST_MAIN(tst_QMdiArea) #include "tst_qmdiarea.moc"