June 14, 2012

tucnak tucnak
Robot Herder
384 posts

[SOLVED] QAction: get way of activation from code

 

AFAIN, signal can be activated with three ways:

  1. QAction::activate(ActionEvent)
  2. Click on it in QMainMenu
  3. Shortcut

I have this code:

  1. MainWindow::MainWindow(QObject *parent = 0)
  2. {
  3.      // ... code skipped
  4.      connect(action, SIGNAL(activated()), this, SLOT(handleAct()));
  5. }
  6.  
  7. void MainWindow::handleAct() {
  8.      QAction * action = (QAction*)sender();
  9.      if(!action)
  10.           return;
  11.      // how to get information about what way action was activated here?
  12. }

 Signature 

Qt Developer (upper-intermediate)
PHP/JS Developer (intermediate)

15 replies

June 14, 2012

Lukas Geyer Lukas Geyer
Dinosaur Breeder
2074 posts

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

June 14, 2012

tucnak tucnak
Robot Herder
384 posts

I want to filter action activation. I want to execute code only if action was called from shortcut.

Lukas Geyer wrote:
I don’t think there is. What do you want to achieve?

 Signature 

Qt Developer (upper-intermediate)
PHP/JS Developer (intermediate)

June 14, 2012

Lukas Geyer Lukas Geyer
Dinosaur Breeder
2074 posts

Is it possible to use QShortcut instead?

June 14, 2012

tucnak tucnak
Robot Herder
384 posts

Now, it’s impossible.

Lukas Geyer wrote:
Is it possible to use QShortcut instead?

 Signature 

Qt Developer (upper-intermediate)
PHP/JS Developer (intermediate)

June 23, 2012

Volker Volker
Robot Herder
5428 posts
tucnak wrote:
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?

June 24, 2012

raaghuu raaghuu
Hobby Entomologist
115 posts

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…

June 25, 2012

Andre Andre
Area 51 Engineer
6077 posts

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.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

June 25, 2012

tucnak tucnak
Robot Herder
384 posts

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

 Signature 

Qt Developer (upper-intermediate)
PHP/JS Developer (intermediate)

June 25, 2012

Andre Andre
Area 51 Engineer
6077 posts

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?

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

June 25, 2012

raaghuu raaghuu
Hobby Entomologist
115 posts

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

June 25, 2012

tucnak tucnak
Robot Herder
384 posts

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.

 Signature 

Qt Developer (upper-intermediate)
PHP/JS Developer (intermediate)

June 25, 2012

raaghuu raaghuu
Hobby Entomologist
115 posts

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

June 25, 2012

tucnak tucnak
Robot Herder
384 posts

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.

 Signature 

Qt Developer (upper-intermediate)
PHP/JS Developer (intermediate)

June 25, 2012

Andre Andre
Area 51 Engineer
6077 posts

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.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

June 25, 2012

tucnak tucnak
Robot Herder
384 posts

Thanks! Here it is!

 Signature 

Qt Developer (upper-intermediate)
PHP/JS Developer (intermediate)

 
  ‹‹ error: ’av_register_all’ was not declared in this scope      Saving rotated QImage ››

You must log in to post a reply. Not a member yet? Register here!