1

I am building a menu and I would like to be able to click both on QAction and QMenu items.

When running exec, nothing happens if I click on a QMenu. Even if I added an action to the QMenu.

Is there a way to do this?

Here is what I tried:

QMenu* menu = new QMenu( "xxx", topMenu );
QAction* action = menu->menuAction();
topMenu->addAction( action );
Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
Denis Rouzaud
  • 2,412
  • 2
  • 26
  • 45

1 Answers1

0

EDIT:

I do not know why exec() makes it different, but to walk-around it you can create subclass of QMenu to be your topMenu so that it handles mouse release event manually like this:

void CustomMenu::mouseReleaseEvent(QMouseEvent *event) {

    QAction *const actionAtEvent = actionAt(event->pos());

    if (actionAtEvent)
        actionAtEvent->trigger();

    QMenu::mouseReleaseEvent(event);
}

Original, not helpfull answer:

It might help you to use QMenu::menuAction() to get associated QAction and connect it's signals.

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
Vincas Dargis
  • 535
  • 5
  • 14
  • Thanks for your reply. I already used menuAction (see updated post), but how can I connect the signal to be used while running exec? – Denis Rouzaud Aug 08 '14 at 07:17
  • I gave it a try without success. The method is being called while the menu is executed, but exec method doesn't return until a proper QAction is clicked. – Denis Rouzaud Aug 11 '14 at 14:52
  • Maybe I do not quite follow what are you trying achieve. I created menu topMenu. It has childMenu, with some actions. I exec() topMenu, popup shows, click on childMenu item to expand it (clock NOT on it's child actions), this method gets called, and childMenu->menuAction() action gets triggered, signal/slot called, menu closes. And you wanted to..? – Vincas Dargis Aug 12 '14 at 15:33
  • Well that's exactly what I am trying to do...I have exactly the same you described except that the menu doesn't close itself. – Denis Rouzaud Aug 13 '14 at 05:16
  • The thing is that I am using an action at the level of the menu itself (by using menu->featureAction() ) – Denis Rouzaud Aug 13 '14 at 05:18