Results 1 to 2 of 2

Thread: how to get both qgraphicsscene and qgraphicsitem to both accept drops

  1. #1
    Join Date
    Jul 2008
    Location
    East Coast, USA
    Posts
    40
    Thanks
    6
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question how to get both qgraphicsscene and qgraphicsitem to both accept drops

    Hi Gang,

    I have some items on a qgraphicsscene and I want to be able to drag and drop an item onto another or onto a blank part of the scene. Depending on whether the drop occurs over another item or over a blank part of the scene two different things happen.

    The way I'm doing this currently is backwards (I think) but is the only way I can get it to work.

    Qt Code:
    1. MyGraphicsScene::dropEvent(....) {
    2. QGraphicsScene::dropEvent(...)
    3. if(!event->isAccepted()) {
    4. event->accept()
    5. return
    6. }
    7. else
    8. do what needs to be done if the drop is on an empty space
    9. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. MyGraphicsItem::dropEvent(...) {
    2. do things
    3. event->ignore();
    4. //This is why we did this. The event comes to us 'accept'ed. If it
    5. //passes into this function we 'ignore' it as a signal to the parent
    6. //(qgraphicsscene) that it should not operate on it. Otherwise
    7. //the event comes with the accept flag and qgraphicsscene acts in it
    8. //This is so ass-backwards. There should be
    9. //a better way of doing this
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 25th December 2008 at 14:13. Reason: changed [qtclass] to [code] tags

  2. #2
    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: how to get both qgraphicsscene and qgraphicsitem to both accept drops

    You can install a sceneEventFilter onto every item and handle the drop there. Furthermore if you don't reimplement dropEvent in the item, the drop should be ignored and handled by the scene. Currently you are trying to handle the drop in the item and in the scene at the same time which makes little sense. You should choose one of the approaches - handle it here (x)or there. If you insist on such strange design then ignoring the event in the item is fine - that's what ignore is meant to be used for. Accept and ignore only marks event as consumed or not, it doesn't determine whether you actually did anything with it.

Similar Threads

  1. QGraphicsView, QGraphicsItem, QGraphicsScene
    By Shuchi Agrawal in forum Newbie
    Replies: 10
    Last Post: 23rd March 2011, 20:50
  2. (QT4.2-RC1) QGraphicsScene QGraphicsView QGraphicsItem
    By antonio.r.tome in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 10:56

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.