PDA

View Full Version : Overriding shortcuts for QGraphicsItems



pherthyl
16th May 2008, 05:36
Hi Everyone.

I've got an app that has the ability to create editable text box graphics items. Now I have the problem that I need actions to work differently depending on whether an item is currently being edited.

For example, the copy action can either copy selected text or copy a whole box, depending on whether text is being edited or not. Same with delete, select all, etc.

So far I've just been creating menu actions that select all items and such, and then overriding QGraphicsScene::keyPressEvent and checking if a box is being edited (and then ignoring keypresses like del). This isn't a very good solution, as I need to check every possible action, and it doesn't handle the scenario of the user selecting actions with the mouse.

So it seems there should be something better. I was looking at the ShortcutOverride events, but I'm not sure how to use them.. My GraphicsScene doesn't get any of those types of events.

IS there something basic I'm missing? How can I make actions do two different things based on an editing text graphics item without checking manually every time? Is there built in support for this? Thanks.

patrik08
16th May 2008, 09:26
To copy a full box you must serialise a class from data
have a look on ...
http://www.qt-apps.org/content/show.php/GraphicsViewEdit+Layer?content=80234
it can drag drop text image inside a bok or self... or drag image from firefox to box..
I write this to build a html editor inside a CMS and as item from the new version of
MiniScribus http://code.google.com/p/fop-miniscribus/ xsl-fo file .

pherthyl
16th May 2008, 16:49
Sorry, my question was probably not very clear. The box copying is not the problem.

The problem is that a QGraphicsTextItem uses several keyboard shortcuts to work with text. For example Ctrl-A will select all text, then you have the standard Ctrl-C/X/V to cut copy paste. Now in my mainwindow I have these same actions defined (cut, copy, select all) and they have the same keyboard shortcut. I defined those actions so that users could manipulate boxes with them. Now my actions are overriding those of the QGraphicsTextItem, so that when I press Ctrl-C while editing text, it triggers my copy action, instead of the text item's.

So it boils down to: Can I avoid my window-level shortcuts overriding the shortcuts in QGraphicsTextItem?

pherthyl
16th May 2008, 23:47
Ok. I ended up solving this in the following way:
Override event(QEvent*) in the QGraphicsView class, check for a ShortcutOverride event, and then if an item is being edited and the shortcut is one of the common text editing shortcuts, accept the event so it won't be handled by the mainwindow.