Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to close context QMenu by dedicated button, while not having actual object of the menu?
Forum Updated to NodeBB v4.3 + New Features

How to close context QMenu by dedicated button, while not having actual object of the menu?

Scheduled Pinned Locked Moved Solved General and Desktop
contex menu
3 Posts 2 Posters 570 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • MasterBLBM Offline
    MasterBLBM Offline
    MasterBLB
    wrote on last edited by MasterBLB
    #1

    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

    1 Reply Last reply
    0
    • SGaistS SGaist

      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.

      MasterBLBM Offline
      MasterBLBM Offline
      MasterBLB
      wrote on last edited by MasterBLB
      #3

      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.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        MasterBLBM 1 Reply Last reply
        1
        • SGaistS SGaist

          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.

          MasterBLBM Offline
          MasterBLBM Offline
          MasterBLB
          wrote on last edited by MasterBLB
          #3

          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.

          1 Reply Last reply
          0
          • MasterBLBM MasterBLB has marked this topic as solved on

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved