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. [SOLVED] QAction: get way of activation from code
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QAction: get way of activation from code

Scheduled Pinned Locked Moved General and Desktop
16 Posts 5 Posters 5.7k Views 1 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.
  • T Offline
    T Offline
    tucnak
    wrote on last edited by
    #1

    AFAIN, signal can be activated with three ways:

    QAction::activate(ActionEvent)

    Click on it in QMainMenu

    Shortcut

    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();
    if(!action)
    return;
    // how to get information about what way action was activated here?
    }
    @

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tucnak
        wrote on last edited by
        #3

        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]

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          Is it possible to use QShortcut instead?

          1 Reply Last reply
          0
          • T Offline
            T Offline
            tucnak
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

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

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

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • R Offline
                R Offline
                raaghuu
                wrote on last edited by
                #7

                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...

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    tucnak
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

                      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?

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        raaghuu
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        0
                        • T Offline
                          T Offline
                          tucnak
                          wrote on last edited by
                          #12

                          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.

                          1 Reply Last reply
                          0
                          • R Offline
                            R Offline
                            raaghuu
                            wrote on last edited by
                            #13

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

                            1 Reply Last reply
                            0
                            • T Offline
                              T Offline
                              tucnak
                              wrote on last edited by
                              #14

                              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.

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                andre
                                wrote on last edited by
                                #15

                                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.

                                1 Reply Last reply
                                0
                                • T Offline
                                  T Offline
                                  tucnak
                                  wrote on last edited by
                                  #16

                                  Thanks! Here it is!

                                  1 Reply Last reply
                                  0

                                  • Login

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