Results 1 to 9 of 9

Thread: GraphicsItem Question

  1. #1
    Join Date
    Apr 2009
    Posts
    12
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default GraphicsItem Question

    I have a GraphicsScene with a bunch of GraphicsItems that can be dragged around in the scene.

    This is what I want to do....

    Suppose I click the GrahicsItem and drag it to a new location...right when i RELEASE the mouse's left click(to drop it)...I want to run a function..

    (In case you were curious of my project..it is a chess project..I want to run the function to center the pieces every time I release the mouse click.)

    Thanks

  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: GraphicsItem Question

    You can override the drag drop mechanism.
    ie. starg drag on mouse press / mouse event, move the item whil dragging, and finally on mouse release check the position of item, and adjust accordingly.

  3. The following user says thank you to aamer4yu for this useful post:

    cwnelatury (13th May 2009)

  4. #3
    Join Date
    Apr 2009
    Posts
    12
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: GraphicsItem Question

    I thought this would only work on widgets....

    How would I do this for a graphicsItem?

  5. #4
    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: GraphicsItem Question

    Well actually you can do it in Scene.
    I had previously done similar thing. I had to move back item if it was dropped in areas not allowed, or align the item to cell if it was dropped somewhere in middle.

    I will brief abt my implementation :

    1) In scene::mousePressEvent capture the position of mouse press. Check if item lies underneath it.

    2) You can start drag on mousepress or mouse move, mouse move is advisable, after the user has picked item and tried to move it.
    To start drag, see QDrag. You will need to populate mime data for it.

    3) Now in drag move event, move the item that started the drag along with the mouse cursor ( You will need to store item pointer in scene which is under drag..

    4) On scene::dropEvent, check the position of item, and do ur calculations.

    Hope this helps

  6. The following user says thank you to aamer4yu for this useful post:

    cwnelatury (13th May 2009)

  7. #5
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: GraphicsItem Question

    Hi

    You can do this in the following way,

    1. first you should set the ItemIsMovable flag

    setFlag(QGraphicsItem::ItemIsMovable,TRUE);


    2. then you gotta reimplement itemChange method.

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

    cwnelatury (13th May 2009)

  9. #6
    Join Date
    Feb 2009
    Posts
    79
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: GraphicsItem Question

    Hi guys, what about the

    QGraphicsItem::MouseRelease

    event ???
    This is exactly what you need!

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

    cwnelatury (14th May 2009)

  11. #7
    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: GraphicsItem Question

    QGraphicsItem::itemChange is also a good option.
    But in this case the item will need to know where it can be placed in the scene.

    Its more appropriate that a higher level ( scene ) decides what items will be placed where.

    Rest , design issue,,, u can choose which suits you.

  12. The following user says thank you to aamer4yu for this useful post:

    cwnelatury (13th May 2009)

  13. #8
    Join Date
    Apr 2009
    Posts
    12
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: GraphicsItem Question

    Sorry..if this question is too basic

    but..I did re implement the QGrahpicsScene

    what I have below isn't working

    Qt Code:
    1. class scene: public QGraphicsScene
    2. {
    3. public:
    4. scene()
    5. {
    6.  
    7. }
    8.  
    9. void mouseReleaseEvent(QMouseEvent *event)
    10. {
    11. clear();
    12. }
    13.  
    14. };
    To copy to clipboard, switch view to plain text mode 


    Everytime I drag a graphicsitem from one place to another and release the mouse button..the scene doesn't clear...

    Please help

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

    Default Re: GraphicsItem Question

    That's because your method is never called. mouseReleaseEvent() in the scene takes a QGraphicsSceneMouseEvent pointer.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Plugin implementation question
    By JPNaude in forum Qt Programming
    Replies: 12
    Last Post: 27th August 2008, 21:24
  2. Useless but curious compiler warning question
    By Raccoon29 in forum General Programming
    Replies: 4
    Last Post: 30th July 2008, 21:46
  3. Question regarding how to paint after zoom.
    By JonathanForQT4 in forum Qt Programming
    Replies: 2
    Last Post: 26th January 2007, 16:34
  4. QThread exit()/quit() question
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 28th August 2006, 15:38

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.