Results 1 to 10 of 10

Thread: Snap-to grid on QGraphicsItem

  1. #1
    Join Date
    May 2006
    Posts
    58
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Snap-to grid on QGraphicsItem

    I have a QGraphicsItem implemented to represent points on a grid. My grid can be of variable resolution, and I would like the ability to enforce snap-to behavior on my points when they're drug around the graphics view. From what I can tell, there is no built-in behavior for this and I will have to impelement my own mouseMoveEvent.

    My initial thought was to somehow "fudge" the QGraphicsSceneMouseEvent before I sent it up to the QGraphicsItem handler, but it seems that I don't have any access methods to set the internal values.

    I checked the mouseMoveEvent code for the QGraphicsItem class, and I really don't want to have to duplicate all of the internal code. Is there any simple solution for offsetting the movements to a grid (i.e. only allow movement in 5 pixel increments etc.), or will I definitely have to re-implement the entirety of the mouseMoveEvent?

  2. #2
    Join Date
    Jan 2006
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Snap-to grid on QGraphicsItem

    How much work does the mouseMoveEvent do in terms of actual movement? You could override the event handler, grab the position beforehand, invoke the parent's handler, and then toy with the position after that has run to snap it to your notion of a grid.

  3. #3
    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: Snap-to grid on QGraphicsItem

    You need to override the mouseMoveEvent of the item, and set the position according to the distance.

    Say in mousepress event u save - m_pos = event->pos();
    then in mouseMoveEvent u cud check
    if( (event->pos() - m_pos) > 5 pixels)
    setPos(); // set the item position.


    Also you can have a look at Drag events to have a finer control of how the items move

  4. #4
    Join Date
    May 2006
    Posts
    58
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Snap-to grid on QGraphicsItem

    Well, the parent handler has a pretty good chunk of code which handles the movement of all items that are currently selected in the view. So, if you have 20 points selected, moving one of them will move all of them by the same x,y offset.

    This is handled in a fairly dense loop. It's not beyond my ability to duplicate and modify this code in my handler, but I really dislike implementing that much code which is dependent on the internals of the API. I would much rather find a solution that allows me to perform some minimal calculations in my hander, and somehow "fool" the parent. If my hander is too complex and dependent on the internals of the GraphicsView/Item, I can easily see it breaking with new versions of QT.

  5. #5
    Join Date
    Jan 2006
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Snap-to grid on QGraphicsItem

    Hmm... Well, could you use the canvas's selected item list, wrap it in a group, and then manage the movement at the level of the group? This way, when the items move, all you have to check is the top-level group and adjust its position and it will filter down, right?

  6. #6
    Join Date
    May 2006
    Posts
    58
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Snap-to grid on QGraphicsItem

    Quote Originally Posted by Shadowfiend View Post
    Hmm... Well, could you use the canvas's selected item list, wrap it in a group, and then manage the movement at the level of the group? This way, when the items move, all you have to check is the top-level group and adjust its position and it will filter down, right?
    That sounds like a possible option...Will there be a lot of overhead caused by creating a destroying a group inside of a mouse event?

  7. #7
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Snap-to grid on QGraphicsItem

    Instead of reimplementing QGraphicsSceneMouseEvent, implement QGraphicsItem::itemChange(). There is an example in the documentation.

  8. #8
    Join Date
    Jan 2006
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Snap-to grid on QGraphicsItem

    There we go. I was going to suggest overriding setPos, but then saw that it wasn't virtual.

    To clarify about my suggestion, however, I wasn't suggesting that you create the group inside the mouse event, but rather when the selection changes.

  9. #9
    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: Snap-to grid on QGraphicsItem

    I second what spud says - reimplement itemChange and make sure that when item coordinates are changed, they get snapped to the grid you want. By the way, I wouldn't represent the grid as a QGraphicsItem. Instead I would draw it inside reimplementation of drawBackground().

  10. #10
    Join Date
    May 2006
    Posts
    58
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Snap-to grid on QGraphicsItem

    Thanks guys. I am getting the behavior I need by implementing itemChange

Similar Threads

  1. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 10:31

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.