[Qt-interest] Standard keys in menus

KC Jones kc at asperasoft.com
Mon Oct 19 02:01:58 CEST 2009


Thanks for the pointers.  It helps me better understand how
actions and menus fit together.

>> Am I really supposed to code slots for each standard key I put in the menu?
>
> Yes. Usually thats easy though, as the delete-code is inside a slot in
> the relevant widget. That means all you need to do is connect the action
> to the right slot of the right widget.

And keep changing that with disconnect()/connect() as focus
moves?  Seems like a lot of unnecessary work.

>> And how am I supposed to implement correct menu enable/disable sensitivity?
>
> Listen to focus changes or enter/leave events and enable/disable the
> actions.

I had not thought about this approach.  But now that I take
a closer look at QAction, I see that the way I thought it
would work is not possible.

What I *thought* I would do would be to subclass QAction and
implement the isEnabled() method to examine the current
widget (or clipboard contents, or...) to determine if the
action was possible at the time the menu is posted.  This
assumed that menus would call isEnabled prior to showing the
menu, and that QAction::isEnabled() was virtual -- which it
is not.

I'm surprised by this.

>> Unless I'm missing something, I would suggest it would be very useful
>> to provide static QAction instances for many of the statdard
>> QKeySequences.
>
> Well, the problem is how should the slot be implemented by Qt in a
> generic way? And how should the action decide which widget to connect
> to, for example if you have multiple text edits. This is part of your
> job as app developer - to put the widgets together and connect them to
> create a meaningful application.

If my approach of specialized QActions were possible, it
would be pretty trivial to implement standard actions for
some of the more important standard key sequences.

But now that I see how these objects work, I see your point.



More information about the Qt-interest-old mailing list