[Qt-interest] Standard keys in menus

Ross Bencina rossb-lists at audiomulch.com
Tue Oct 20 03:27:16 CEST 2009


Hi, interesting thread, coming in a bit late on this..

I think it depends on what types widgets you're dealing with and how many 
there are. I don't think Qt has a high-level abstraction for what you're 
trying to do, although you could build one.

Here's a couple of of ideas:

- If you're only dealing with a few standard widgets (say subclasses of 
QLineEdit and QTextEdit) you could just have a single signal handler in your 
Main Window for each signal (cut/copy/paste etc), use qobject_cast to work 
out which type QApplication::focusWidget() is, and then invoke the 
appropriate method directly on QApplication::focusWidget() . A 
QApplication::focusChanged handler could be used to only enable your actions 
when the focus changes. So long is you're ok with Cut and Delete/Clear being 
enabled when the text selection is empty this is a pretty light-weight 
approach. It could be made more complete if you're prepared to hook into 
selectionChanged events for the current focus widget.

- If your application consists mainly of custom views/editors/widgets 
consider having a separate abstract Selection object hierarchy, with an 
application global currentSelection and selection changed notifications. 
When a view gets focus it sets currentSelection object to the Selection 
subclass associated with the view. Then your Edit menu actions just invoke 
methods on the currentSelection without needing to know what selection 
they're operating on. This is the way I do it. Selection sends notifications 
when it changes, and virtual methods for copy/canCopy, paste/canPaste etc.

HTH

Ross.




===================================
AudioMulch 2.0 is here!

http://www.audiomulch.com
----- Original Message ----- 
From: "KC Jones" <kc at asperasoft.com>
To: <qt-interest at trolltech.com>
Sent: Monday, October 19, 2009 4:54 AM
Subject: [Qt-interest] Standard keys in menus


> First post here, pardon me if this is a well known issue.  I searched
> the archives and poured over the docs to no avail, yet I can't believe
> I'm the first person to run into this...
>
> Is there a standard way to handle standard key actions in menus?
>
> I'm building my application menu and I need to add items for various
> standard keys.  My app does not implement any custom actions for these
> keys and relies happily on Qt to implement the functionality on a per
> widget basis.  Let's consider the delete action (just because its
> shortcut key display is non-trivial, Cut, Copy, Paste are also
> relevant here).
>
> I see the standard key definition, QKeySequence::Delete
> I add it to my edit menu by creating a delete action with the shortcut
> set to QKeySequence::Delete and the shortcut shows correctly.  Nice.
> When I hit the DEL key itself, it works as expected.
> So far so good.
>
> But if I select the menu item nothing happens since I have not defined
> that slot.
> And when focus is on some widget that does not support delete, the
> menu item is not disabled.
>
> Am I really supposed to code slots for each standard key I put in the 
> menu?
> And how am I supposed to implement correct menu enable/disable 
> sensitivity?
>
> Why is the Menus example so lame in this regard?  It "implements" a
> 'Cut' menu item that does nothing but update a status bar.
>
> Unless I'm missing something, I would suggest it would be very useful
> to provide static QAction instances for many of the statdard
> QKeySequences.
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest 




More information about the Qt-interest-old mailing list