Important: Please read the Qt Code of Conduct -

[SOLVED] QAction: get way of activation from code

  • AFAIN, signal can be activated with three ways:


    Click on it in QMainMenu


    I have this code:
    MainWindow::MainWindow(QObject *parent = 0)
    // ... code skipped
    connect(action, SIGNAL(activated()), this, SLOT(handleAct()));

    void MainWindow::handleAct() {
    QAction * action = (QAction*)sender();
    // how to get information about what way action was activated here?

  • I don't think there is. What do you want to achieve?

  • I want to filter action activation. I want to execute code only if action was called from shortcut.
    [quote author="Lukas Geyer" date="1339674244"]I don't think there is. What do you want to achieve?[/quote]

  • Is it possible to use QShortcut instead?

  • Now, it's impossible.
    [quote author="Lukas Geyer" date="1339676153"]Is it possible to use QShortcut instead?[/quote]

  • [quote author="tucnak" date="1339674423"]I want to filter action activation. I want to execute code only if action was called from shortcut.

    Then why do you put it into a menu at all?

  • you could attach the particular code to be executed to Key Press Event so that when the shortcut combo is pressed only then that pice of code is executed...

  • If you need a different code path if an action is triggered in different ways, then it obviously does not belong in the same action. My solution would be to use different QActions then, that perhaps trigger calling the same code with different arguments.

  • Thanks a lot. But I want to store QAction in application menu and I want it to be executed only via shortcut (without click).

  • I really don't get that requirement. What do you mean by that exactly? What is "storing QAction in application menu"? Do you mean you want to show a menu item for your action? If so, why only execute it via a shortcut? That seems strange... And if the shortcut needs to be different than the menu click, why does it need to be the same QAction?

  • yeah... valid questions... why place it there(in the menu) if you don't want it clicked?

  • Sorry. I have mistake. I need to paint my action as menu item in main menu.

    I need it to write at the right of the item - shortcut (Ctrl+V). But I need it NOT be executed on shortcut.

  • why?!?!!why make a shortcut if you don't want it to be executed?0_o

  • I want it to be painted.

    I use external library, and when I call Ctrl+H shortcut it calls two times because I handle it in actions.

    Problem solved. Thanks.

  • Hmmm... It is starting to make some kind of sense to me. Let me try to re-word the requirement, so you can tell me if I understand you correctly.

    You need to have an action A in a menu. When A is triggered by clicking on it, something cool happens. However, this item in the menu also needs to display a shortcut, say CTRL+A. However, due to the nature of the application, when this shortcut is used, the actual action being performed depends on the context of where the shortcut was triggered from. So, the action is actually slightly different when the menu or the shortcut triggers it. For discoverabilty of the shortcut, it is still important to advertise the shortcut for the action in the menu.

    The problem tucnak then faces, is that if you actualy put the shortcut on the QAction, the QAction is triggered when the shortcut is pressed, while the subtely different version should be triggered instead. There is no way to figure out if a mouse press or a keyboard shortcut triggered the action in the first place, so you cannot filter on that in the slot connected to the QAction::triggered() signal.

    Do I get that right?

    If so, the option that comes to mind is to install an application wide event filter, and listen for the key shortcut key combination that way. You can filter out the event before it reaches the code that triggers the QAction to be fired.

  • Thanks! Here it is!

Log in to reply