Results 1 to 9 of 9

Thread: Drar and Drop on QGraphicsView

  1. #1
    Join Date
    Apr 2008
    Posts
    26
    Thanks
    1

    Default Drar and Drop on QGraphicsView

    Hello. Im dragging from widget to QGraphicsView. Is it correct that the only way to accept drop is to create a QGraphicsItem in QGraphicsScene connected to my QGraphicsView and accept drops with this item?
    I mean is there no way to accept drag and drop directly by QGraphicsScene?

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drar and Drop on QGraphicsView

    I guess u are on right track..
    graphics scene accepting drops by default doesnt make sense, as graphics view framework is not designed for a particular thing... there can be many user defined items, operations.

  3. #3
    Join Date
    Apr 2008
    Posts
    26
    Thanks
    1

    Default Re: Drar and Drop on QGraphicsView

    Quote Originally Posted by aamer4yu View Post
    I guess u are on right track..
    graphics scene accepting drops by default doesnt make sense, as graphics view framework is not designed for a particular thing... there can be many user defined items, operations.
    I see.. Not too logical in my opinion.. I dont want graphics scene to accept drops by default. I wanted to overload dropEvent function.
    For example im doing something like Form designer. I want to drag some widget description in graphics view and when i drop i want corresponding graphic item added to graphic scene.
    But now at start i must add some basic graphics item to scene, track scene resize and resize this widget as well and every drop is processed through this widget..
    But, anyway, this is a solution. Thanks.

  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: Drar and Drop on QGraphicsView

    Quote Originally Posted by aamer4yu View Post
    I guess u are on right track..
    graphics scene accepting drops by default doesnt make sense, as graphics view framework is not designed for a particular thing... there can be many user defined items, operations.
    No, this is wrong. You can subclass QGraphicsScene and reimplement the appropriate event handler.

  5. #5
    Join Date
    Apr 2008
    Posts
    26
    Thanks
    1

    Default Re: Drar and Drop on QGraphicsView

    Quote Originally Posted by wysota View Post
    No, this is wrong. You can subclass QGraphicsScene and reimplement the appropriate event handler.
    Unfortunately it won't help as QGraphicsScene::dropEvent redirects drop to the first graphics item that accept drop, so u should reimplement this item's dropEvent if you want to handle drops from foreign widgets.

  6. #6
    Join Date
    Apr 2008
    Posts
    26
    Thanks
    1

    Default Re: Drar and Drop on QGraphicsView

    Well, i added graphics item, reimplemented dragEnterEvent and dropEvent and drop stil doesnt happen ((
    Ive attached a small example. I'd appreciate if someone tells me what i'm doing wrong.
    Attached Files Attached Files

  7. #7
    Join Date
    Apr 2008
    Posts
    26
    Thanks
    1

    Default Re: Drar and Drop on QGraphicsView

    added these 3 functions and finally it works
    Qt Code:
    1. void dragEnterEvent ( QGraphicsSceneDragDropEvent * event )
    2. {
    3. cout << "drag entered in graphics item 2" << endl;
    4. event->acceptProposedAction();
    5. }
    6. void dropEvent ( QGraphicsSceneDragDropEvent * event )
    7. {
    8. cout << "dropped in graphics item 2" << endl;
    9. event->acceptProposedAction();
    10. }
    11.  
    12. void dragMoveEvent ( QGraphicsSceneDragDropEvent * event )
    13. {
    14. cout << "drag moved in graphics item 2" << endl;
    15. event->acceptProposedAction();
    16. }
    To copy to clipboard, switch view to plain text mode 
    Still maybe there is a way to accept drops by graphics scene directly?

  8. #8
    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: Drar and Drop on QGraphicsView

    Quote Originally Posted by rippa View Post
    Unfortunately it won't help as QGraphicsScene::dropEvent redirects drop to the first graphics item that accept drop, so u should reimplement this item's dropEvent if you want to handle drops from foreign widgets.
    No, because the method is virtual so you can reimplement it. If you don't call the base class implementation, no item will get the event.

  9. #9
    Join Date
    Apr 2008
    Posts
    26
    Thanks
    1

    Default Re: Drar and Drop on QGraphicsView

    Yes, did it already ) The most confusing moment was when you want to drop on "normal" widget you need to reimplement dropEvent and dragEnterEvent, but when you drop on QGraphcisView, you need to reimplement dragMoveEvent too.

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.