Results 1 to 12 of 12

Thread: Deleting QGraphicsItems with Qt::Key_Delete

  1. #1
    Join Date
    Jul 2008
    Posts
    27
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Deleting QGraphicsItems with Qt::Key_Delete

    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.
    If God has friends ,then I cant be God.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Deleting QGraphicsItems with Qt::Key_Delete

    you can try to set event filter for QGraphicsView and catch QKeyEvent and then process needed keys.

  3. #3
    Join Date
    Jul 2008
    Posts
    27
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Deleting QGraphicsItems with Qt::Key_Delete

    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?
    If God has friends ,then I cant be God.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Deleting QGraphicsItems with Qt::Key_Delete

    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.

  5. #5
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Deleting QGraphicsItems with Qt::Key_Delete

    I delete item on this way :


    Qt Code:
    1. /* on item before delete */
    2.  
    3. setData(nummerhash,"ping");
    4. GraphicsScene *sc = qobject_cast<GraphicsScene *>(scene());
    5. sc->removeIdItem(AbsoluteLayerRemoveHashID);
    6. }
    7.  
    8.  
    9. /* send a ID to this to remove it.. */
    10. void GraphicsScene::removeIdItem( const int idchunk )
    11. {
    12. const QList<QGraphicsItem*> items = QGraphicsScene::items();
    13. QList<QGraphicsItem*> subLevelItems;
    14. /* loop all elemenst */
    15. foreach(QGraphicsItem* item, items)
    16. {
    17. subLevelItems << item;
    18.  
    19. if (item->parentItem())
    20. {
    21. QList<QGraphicsItem *> des = item->childItems();
    22. foreach(QGraphicsItem* de,des)
    23. {
    24. subLevelItems << de;
    25. }
    26. }
    27. }
    28. foreach(QGraphicsItem* it, subLevelItems) {
    29. /* serialize to undo ?? */
    30. const QString chunk = it->data(idchunk).toString();
    31. if (chunk.size() > 3) {
    32. delete it;
    33. }
    34. }
    35. }
    36.  
    37. /* to remove all it running ok from qt4.4 > */
    38. void GraphicsScene::clear()
    39. {
    40. clearSelection();
    41. QGraphicsScene::clear();
    42. storno();
    43. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jul 2008
    Posts
    27
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Deleting QGraphicsItems with Qt::Key_Delete

    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).
    Last edited by Benne Gesserit; 1st September 2008 at 19:04.
    If God has friends ,then I cant be God.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Deleting QGraphicsItems with Qt::Key_Delete

    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

  8. The following user says thank you to wysota for this useful post:

    Benne Gesserit (2nd September 2008)

  9. #8
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Deleting QGraphicsItems with Qt::Key_Delete

    Quote Originally Posted by wysota View Post
    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 );

    QGraphicsItem
    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().

  10. The following user says thank you to patrik08 for this useful post:

    Benne Gesserit (2nd September 2008)

  11. #9
    Join Date
    Jul 2008
    Posts
    27
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Deleting QGraphicsItems with Qt::Key_Delete

    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.
    If God has friends ,then I cant be God.

  12. #10
    Join Date
    Jul 2008
    Posts
    27
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Deleting QGraphicsItems with Qt::Key_Delete

    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.
    If God has friends ,then I cant be God.

  13. #11
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Deleting QGraphicsItems with Qt::Key_Delete

    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..
    QGraphicsItem::keyPressEvent()

    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....?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...
    search
    qreal GraphicsScene::zmax()
    qreal GraphicsScene::zmin()

    on file

    http://fop-miniscribus.googlecode.co...ibe_Parser.cpp

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

  14. #12
    Join Date
    Mar 2009
    Posts
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Deleting QGraphicsItems with Qt::Key_Delete

    First of all look up into ur code if you are any how capturing Delete key. Check if you have used :

    setShortcut(tr("Delete"));

    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.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.