Results 1 to 4 of 4

Thread: Graphics item position after drag

  1. #1
    Join Date
    Mar 2009
    Posts
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Graphics item position after drag

    Hi,

    I've been trying to implement drag&drop functionality in my custom QGraphicsItem object. I've implemented three derived methods: mousePressEvent, mouseReleaseEvent and mouseMoveEvent.

    If I simply click an item in scene, press and release events are called. If I try to drag an item, press and move events are called, but release event is not. Why is that?

    The problem is that I'd need to get the scene coordinates where I drop the item after dragging. One solution might be to get QCursor::pos() and map it to scene coordinate system, but I guess there is an easier way to do this?

  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: Graphics item position after drag

    What are you trying to do with drag and drop ?
    If you simply want to move the item in the scene, you dont need to implement drag drop yourself... you just need to set the flags,,QGraphicsItem::setFlags , QGraphicsItem::ItemIsMovable


    But if your requirement is implementing drag drop yourself, have a look at -
    QGraphicsItem::dragEnterEvent
    QGraphicsItem::dragLeaveEvent
    QGraphicsItem::dragMoveEvent
    QGraphicsItem::dropEvent
    Hope this helps

  3. #3
    Join Date
    Mar 2009
    Posts
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Graphics item position after drag

    Thanks for the info, it seems that this was the easy solution I was looking for

    I dumbed the mousePressEvent etc. methods and simply set appropriate flag in my graphic item's ctor and... well, it kinda worked. I tested with a simple rectangle, it can be dragged but it leaves a trail in the scene (see attached capture). The trail disappears once the screen is refreshed, e.g. focusing other window and re-focusing my application. Dragging a pixmap didn't have this kind of effect... I'll keep investigating this.
    Attached Images Attached Images

  4. #4
    Join Date
    Mar 2009
    Posts
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Graphics item position after drag

    ...and a second after I clicked the Submit button, I realized where the problem is...

    The black rectangle in previous post is drawn as a border for pixmap. However, my boundingRect function returned the rect for pixmap only and didn't calculate the border. That was also the reason why it worked well with pixmap... Once I fixed boundingRect return value it started to work slightly better

    There are still some problems when there are several items shown, but I'll keep on digging.

  5. The following user says thank you to Miihkali for this useful post:

    ensky_cy (26th November 2009)

Similar Threads

  1. Replies: 1
    Last Post: 25th February 2009, 01:34
  2. Replies: 7
    Last Post: 8th September 2006, 17:19
  3. drag and drop with item views
    By castorvert in forum Qt Programming
    Replies: 14
    Last Post: 27th March 2006, 11:12
  4. how change the QListBox item position by pixel
    By roy_skyx in forum Qt Programming
    Replies: 2
    Last Post: 20th January 2006, 02:34

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.