From c811e2d991a6047b7cbef6e4e9d3675452448400 Mon Sep 17 00:00:00 2001 From: nerix Date: Sun, 29 Oct 2023 20:24:38 +0100 Subject: [PATCH] refactor: Change lifetime of context menus (#4924) --- CHANGELOG.md | 1 + src/widgets/dialogs/UserInfoPopup.cpp | 11 ++--------- src/widgets/helper/ChannelView.cpp | 23 +++++------------------ 3 files changed, 8 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 38ba4b1a4..350a25c95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ - Dev: Improve performance by reducing repaints caused by selections. (#4889) - Dev: Removed direct dependency on Qt 5 compatibility module. (#4906) - Dev: Refactor `DebugCount` and add copy button to debug popup. (#4921) +- Dev: Changed lifetime of context menus. (#4924) ## 2.4.6 diff --git a/src/widgets/dialogs/UserInfoPopup.cpp b/src/widgets/dialogs/UserInfoPopup.cpp index 77beea3e8..2c48b134e 100644 --- a/src/widgets/dialogs/UserInfoPopup.cpp +++ b/src/widgets/dialogs/UserInfoPopup.cpp @@ -278,15 +278,8 @@ UserInfoPopup::UserInfoPopup(bool closeAutomatically, QWidget *parent, return; } - static QMenu *previousMenu = nullptr; - if (previousMenu != nullptr) - { - previousMenu->deleteLater(); - previousMenu = nullptr; - } - - auto menu = new QMenu; - previousMenu = menu; + auto *menu = new QMenu(this); + menu->setAttribute(Qt::WA_DeleteOnClose); auto avatarUrl = this->avatarUrl_; diff --git a/src/widgets/helper/ChannelView.cpp b/src/widgets/helper/ChannelView.cpp index c80b1ae50..74bfe9652 100644 --- a/src/widgets/helper/ChannelView.cpp +++ b/src/widgets/helper/ChannelView.cpp @@ -73,19 +73,13 @@ namespace { MessageElementFlags creatorFlags, QMenu &menu) { auto *openAction = menu.addAction("&Open"); - auto openMenu = new QMenu; + auto *openMenu = new QMenu(&menu); openAction->setMenu(openMenu); auto *copyAction = menu.addAction("&Copy"); - auto copyMenu = new QMenu; + auto *copyMenu = new QMenu(&menu); copyAction->setMenu(copyMenu); - // see if the QMenu actually gets destroyed - QObject::connect(openMenu, &QMenu::destroyed, [] { - QMessageBox(QMessageBox::Information, "xD", "the menu got deleted") - .exec(); - }); - // Add copy and open links for 1x, 2x, 3x auto addImageLink = [&](const ImagePtr &image, char scale) { if (!image->isEmpty()) @@ -2099,15 +2093,8 @@ void ChannelView::addContextMenuItems( const MessageLayoutElement *hoveredElement, MessageLayoutPtr layout, QMouseEvent *event) { - static QMenu *previousMenu = nullptr; - if (previousMenu != nullptr) - { - previousMenu->deleteLater(); - previousMenu = nullptr; - } - - auto menu = new QMenu; - previousMenu = menu; + auto *menu = new QMenu(this); + menu->setAttribute(Qt::WA_DeleteOnClose); // Add image options if the element clicked contains an image (e.g. a badge or an emote) this->addImageContextMenuItems(hoveredElement, layout, event, *menu); @@ -2416,7 +2403,7 @@ void ChannelView::addCommandExecutionContextMenuItems( menu.addSeparator(); auto *executeAction = menu.addAction("&Execute command"); - auto cmdMenu = new QMenu; + auto *cmdMenu = new QMenu(&menu); executeAction->setMenu(cmdMenu); for (auto &cmd : cmds)