View Full Version : Deleting QGraphicsItems with Qt::Key_Delete

Benne Gesserit
1st September 2008, 18:42
My question must be very stupid , but I cant find the answer (I have searched for a while ,sorry if it was just "right there" and I havent seen it ) .
Ok,lets say we have a normal QGraphicsScene ,with a normal QGrraphicsView and some items .If I am not wrong ,by default when you select few items and press Qt::Key_Delete (Del ,right?) the get deleted ,dont they? Well , my question is ,who handles that key press event? I needed to catch whenever some of my items are deleted before the destructor is called .I thought that maybe an event was sent to them ,but I think actually there is not such event .So my second option was tracking the few ways I ll program and right now I was loocking for where is Qt::Key_Delete press event catched.

Thank you verry much.

1st September 2008, 18:47
you can try to set event filter for QGraphicsView and catch QKeyEvent and then process needed keys.

Benne Gesserit
1st September 2008, 19:08
Yes ,but the thing is somewhere there is some code that I could use .I mean ,I could install a filter ,catch Key_Delete presses and do whatever I need to do .I dont know if that would work ,because it is not the view the one who is catching the press event .The default QGrapchicsView just handles up,down and right ,left presses .Maybe the event would happen before it s propagated to the view.
Anyway , (I ll try that if I dont find anything more) ,I am still curious , who is deleting the items? :D

1st September 2008, 19:37
As far as I know items don't get deleted by themselves, there must be an event handler active that performs that. It is either the item itself, the scene or the view.

1st September 2008, 19:52
I delete item on this way :

/* on item before delete */

GraphicsScene *sc = qobject_cast<GraphicsScene *>(scene());

/* send a ID to this to remove it.. */
void GraphicsScene::removeIdItem( const int idchunk )
const QList<QGraphicsItem*> items = QGraphicsScene::items();
QList<QGraphicsItem*> subLevelItems;
/* loop all elemenst */
foreach(QGraphicsItem* item, items)
subLevelItems << item;

if (item->parentItem())
QList<QGraphicsItem *> des = item->childItems();
foreach(QGraphicsItem* de,des)
subLevelItems << de;
foreach(QGraphicsItem* it, subLevelItems) {
/* serialize to undo ?? */
const QString chunk = it->data(idchunk).toString();
if (chunk.size() > 3) {
delete it;

/* to remove all it running ok from qt4.4 > */
void GraphicsScene::clear()

Benne Gesserit
1st September 2008, 19:53
I have done some debugging .I tried to catch key press event in the scene .The keyPressEvent() handler got the event if I pressed 'P' for example ,but I didnt seem to be called when I pressed Del .
As I have said before ,the default view does something just for up,down,left and right keys .
If it was up to the items .....how would they do it? They cant delete themselves ,and there is not deletelater() or stuff for items.....
So I think it must be the scene ,but ...

void QGraphicsScene::keyPressEvent ( QKeyEvent * keyEvent ) [virtual protected]
This event handler, for event keyEvent, can be reimplemented in a subclass to receive keypress events. The default implementation forwards the event to current focus item.
I am really confused.
Edit: Yes Patrik ,I think I have seen a thread where you posted talking about deleting items too....but I think I dont need to do that .I just need to know when they are going to be deleted (how are they deleted when you press del).

1st September 2008, 20:11
Can't you look at the source code of the application? I seem to be unable to understand what the problem is... You're using a subclass of either scene or the view or items, so simply look at their source code and see which of them handles Qt::Key_Del. The order of event propagation is item->underlying item-> ... ->scene->view

1st September 2008, 20:24
Can't you look at the source code of the application? I seem to be unable to understand what the problem is... You're using a subclass of either scene or the view or items, so simply look at their source code and see which of them handles Qt::Key_Del. The order of event propagation is item->underlying item-> ... ->scene->view

whitout QGraphicsItem::ItemIsFocusable scene dont send keyevent!
QGraphicsItem::setFlags(this->flags() | QGraphicsItem::ItemIsFocusable );


The item supports keyboard input focus (i.e., it is an input item). Enabling this flag will allow the item to accept focus, which again allows the delivery of key events to QGraphicsItem::keyPressEvent() and QGraphicsItem::keyReleaseEvent().

Benne Gesserit
1st September 2008, 20:27
I have looked at the source code of the aplication (I am developing the aplication) ,I looked at the code of the view ,the scene,the items....And I cant see who handles that event.
Items? The default implementation does nothing ,and I havent implemented a handler for that key press (I assume it is a key press event).
View?I havent changed it s default behaviour ,and it does nothing with key_del.
So....the scene? As I have said,I think QGraphicsScene::keyPressEvent() is not called when I press Del ,maybe de debugger just doesnt stop for somereason ,I dont know.
My problem is that ,i cant see who deletes selected items when you press del .Maybe it s stupid....because I think it should be an easy thing to find out with de default view and scene.

Benne Gesserit
2nd September 2008, 09:47
Ok ,my stupid question has an easy answer (maybe you already told me but I didnt get it) : the default view,scene and item dont do anything when you press delete .I thought they did :( .Excuse everyone for my silly words ,my brain wasnt working properly yesterday.

2nd September 2008, 10:42
if you press delete on keyboard scene waht must remove?
i suppose one item .. the item that having focus...

If you dont subclass this item keyboard not responding from item...
Only qgraphictextimem having keyboard events..

if you subclass this item and catch all event by

bool QGraphicsItem::sceneEvent ( QEvent * event ) [virtual protected]

you dont see a QKeyEvent exept by qgraphictextimem...

the doc say to append flag QGraphicsItem::setFlags(this->flags() | QGraphicsItem::ItemIsFocusable );

and now you see QKeyEvent

I working now 60 day on QGraphicsItem and QGraphicView ...
My Scribe beta editor http://www.qt-apps.org/content/show.php/Fop+MiniScribus+XSL-Fo?content=67552 i subclass QGraphicsRectItem to build in a text editable layer

/* floating layer*/
class AbsoluteLayer : public QObject, public QGraphicsRectItem
/* scroll page auto flow text */
class TextLayer : public QObject, public QGraphicsRectItem

and his rect() change any time the document size QGraphicsTextItem can not set dimension correct and mouse event on shape not responding if text is small!

also subclass the item and append the flag QGraphicsItem::ItemIsFocusable

If you force delete from GraphicsScene you must find out the zValue() max and minimum
to find the active item by pos... to take one item active item can retry moore...
qreal GraphicsScene::zmax()
qreal GraphicsScene::zmin()

on file


and read
/* filter only item a top Zindex / zValue */
bool GraphicsScene::WakeUp( const QPointF incomming )

5th December 2009, 11:28
First of all look up into ur code if you are any how capturing Delete key. Check if you have used :


any where in your code.

As this will capture Delete key on application level prevent the KeyPressEvent to capture Qt::Key_Delete. I have also faced the similar problem. The moment i commented this piece of line KeyPressEvent captured the Qt::Key_Delete. Thiswas the only possible way we were not able to capture key Qt::Key_Delete with keypressevent.