How to close context QMenu by dedicated button, while not having actual object of the menu?
-
Hello mates
I wonder how to get access to actual object of context menu if the menu is created via QWidget::addAction(QAction*), like this:
//setup context menu for the design area designArea->setContextMenuPolicy(Qt::ActionsContextMenu); auto menuTitle = new QWidgetAction(this); auto title = new QLabel("<b>Design area menu</b>"); title->setMinimumHeight(16); auto closeButton = new QPushButton(title); closeButton->setIcon(QIcon(Icons::closeButton)); closeButton->setFixedSize(16, 16); closeButton->move(125, 0); title->setAlignment(Qt::AlignCenter); menuTitle->setDefaultWidget(title); designArea->addAction(menuTitle); auto createContextMenus = [this](const QString &text, QObject *receiver, const char *slot)->void { QAction *action = new QAction(text, designArea); designArea->addAction(action); connect(action, SIGNAL(triggered()), receiver, slot); }; createContextMenus("New mech", this, SLOT(addNewMech())); createContextMenus("Load mech", this, SLOT(loadMech())); createContextMenus("Cascade designs", designArea, SLOT(cascadeSubWindows())); createContextMenus("Save all", this, SLOT(saveAll())); createContextMenus("Minimize all", this, SLOT(minimizeAll())); createContextMenus("Restore all", this, SLOT(restoreAll())); createContextMenus("Close all", designArea, SLOT(closeAllSubWindows()));
I need that for connection of that closeButton to ideally QMenu::close(), but if there is other way to make the button work it'd be great. In assistant however I haven't found any slot in QWidget like closeMenu().
Any hint will be most appreciated.Windows 10, Qt 5.15
-
Hello @SGaist
That's correct. Sadly, in my use case usual way of closing the context menu by click outside of its bounds has side effects, as in close neighborhood of the menu there are many QWidgets whose react to mouse clicks. Hence the need of dedicated close/cancel button.However, I've finally found a hint in Assistant "If the QWidgetAction fires the triggered() signal, the menu will close.".
So, the problem is solved for my case, the solution is just:connect(closeButton, &QPushButton::clicked, menuTitle, &QWidgetAction::triggered);
but thank you mate nonetheless for trying to help.
-
Hi,
Do you mean some sort of "cancel" button ?
Usually, if a user does not want to use any of the actions proposed by the contextual menu, they would simply click outside to dismiss it.
Otherwise, you can call the hide method. -
Hello @SGaist
That's correct. Sadly, in my use case usual way of closing the context menu by click outside of its bounds has side effects, as in close neighborhood of the menu there are many QWidgets whose react to mouse clicks. Hence the need of dedicated close/cancel button.However, I've finally found a hint in Assistant "If the QWidgetAction fires the triggered() signal, the menu will close.".
So, the problem is solved for my case, the solution is just:connect(closeButton, &QPushButton::clicked, menuTitle, &QWidgetAction::triggered);
but thank you mate nonetheless for trying to help.
-
M MasterBLB has marked this topic as solved on