diff --git a/src/widgets/widgets/qmenu.cpp b/src/widgets/widgets/qmenu.cpp index 98dc6d0a11..d62c010c60 100644 --- a/src/widgets/widgets/qmenu.cpp +++ b/src/widgets/widgets/qmenu.cpp @@ -72,6 +72,7 @@ #include #include #include +#include QT_BEGIN_NAMESPACE @@ -127,7 +128,7 @@ public: setParent(parentWidget, Qt::Window | Qt::Tool); setAttribute(Qt::WA_DeleteOnClose, true); setAttribute(Qt::WA_X11NetWmWindowTypeMenu, true); - setWindowTitle(p->windowTitle()); + updateWindowTitle(); setEnabled(p->isEnabled()); #if QT_CONFIG(cssparser) setStyleSheet(p->styleSheet()); @@ -165,6 +166,15 @@ public: } } + void updateWindowTitle() + { + Q_D(QTornOffMenu); + if (!d->causedMenu) + return; + const QString &cleanTitle = QPlatformTheme::removeMnemonics(d->causedMenu->title()).trimmed(); + setWindowTitle(cleanTitle); + } + public slots: void onTrigger(QAction *action) { d_func()->activateAction(action, QAction::Trigger, false); } void onHovered(QAction *action) { d_func()->activateAction(action, QAction::Hover, false); } @@ -183,6 +193,10 @@ void QMenuPrivate::init() q->setAttribute(Qt::WA_X11NetWmWindowTypePopupMenu); defaultMenuAction = menuAction = new QAction(q); menuAction->d_func()->menu = q; + QObject::connect(menuAction, &QAction::changed, [=] { + if (!tornPopup.isNull()) + tornPopup->updateWindowTitle(); + }); q->setMouseTracking(q->style()->styleHint(QStyle::SH_Menu_MouseTracking, 0, q)); if (q->style()->styleHint(QStyle::SH_Menu_Scrollable, 0, q)) { scroll = new QMenuPrivate::QMenuScroller; diff --git a/tests/auto/widgets/widgets/qmenu/qmenu.pro b/tests/auto/widgets/widgets/qmenu/qmenu.pro index 7c1315afa8..55fff01138 100644 --- a/tests/auto/widgets/widgets/qmenu/qmenu.pro +++ b/tests/auto/widgets/widgets/qmenu/qmenu.pro @@ -1,6 +1,6 @@ CONFIG += testcase TARGET = tst_qmenu -QT += widgets testlib +QT += gui-private widgets testlib SOURCES += tst_qmenu.cpp macx:{ OBJECTIVE_SOURCES += tst_qmenu_mac.mm diff --git a/tests/auto/widgets/widgets/qmenu/tst_qmenu.cpp b/tests/auto/widgets/widgets/qmenu/tst_qmenu.cpp index e5e2e157c5..f0fb7bc367 100644 --- a/tests/auto/widgets/widgets/qmenu/tst_qmenu.cpp +++ b/tests/auto/widgets/widgets/qmenu/tst_qmenu.cpp @@ -46,6 +46,8 @@ #include #include +#include + Q_DECLARE_METATYPE(Qt::Key); Q_DECLARE_METATYPE(Qt::KeyboardModifiers); @@ -622,6 +624,7 @@ void tst_QMenu::tearOff() QScopedPointer menu(new QMenu(&widget)); QVERIFY(!menu->isTearOffEnabled()); //default value menu->setTearOffEnabled(true); + menu->setTitle(QLatin1String("Same &Menu")); menu->addAction("aaa"); menu->addAction("bbb"); QVERIFY(menu->isTearOffEnabled()); @@ -644,6 +647,19 @@ void tst_QMenu::tearOff() QVERIFY(torn); QVERIFY(torn->isVisible()); + // Check menu title + const QString cleanTitle = QPlatformTheme::removeMnemonics(menu->title()).trimmed(); + QCOMPARE(torn->windowTitle(), cleanTitle); + + // Change menu title and check again + menu->setTitle(QLatin1String("Sample &Menu")); + const QString newCleanTitle = QPlatformTheme::removeMnemonics(menu->title()).trimmed(); + QCOMPARE(torn->windowTitle(), newCleanTitle); + + // Clear menu title and check again + menu->setTitle(QString()); + QCOMPARE(torn->windowTitle(), QString()); + menu->hideTearOffMenu(); QVERIFY(!menu->isTearOffMenuVisible()); QVERIFY(!torn->isVisible());