Results 1 to 4 of 4

Thread: QGraphicsItem / QGraphicsProxyWidget: how to snap to grid

  1. #1
    Join Date
    Jan 2008
    Posts
    56
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QGraphicsItem / QGraphicsProxyWidget: how to snap to grid

    Hi all,

    I have a QGraphicsScene with some QGraphicsProxyWidgets that the user can move around with the mouse.
    I'd like to snap the widgets to an (invisible) grid.
    I tried to reimplement mouseReleaseEvent in a way that the event->pos() is set to a multiple of 5 but that doesn't work properly.
    Any hint where to look for a solution?

    Rainer

  2. #2
    Join Date
    Jan 2009
    Location
    Czech Republic
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsItem / QGraphicsProxyWidget: how to snap to grid

    Well... I believe that there are multiple solutions I will mention two of them, probably the best one is to use QGraphicsGridLayout and calculate position (which I believe is what you tried to do, do not know what might go wrong), or create "dummy" items of the same size also placed in grid and use them to catch the drops and move the actual items accordingly.

  3. #3
    Join Date
    Jan 2008
    Posts
    56
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsItem / QGraphicsProxyWidget: how to snap to grid

    Thanks Palmik for your suggestions

    I had a look at QGraphicsGridLayout and started implementing it.
    However, there are still some issues that I need to think about.

    The idea is to have an area where several small widgets can be arranged "freely". Maybe, there will be a photo in the background and the widgets (which represent devices in the real world) are arranged according to their position on the photo.
    What I want to achieve is, that two widgets are not placed on top of eachother and that they are aligned to a grid of, say, 5 pixel to make it look neat.
    Therefore, my idea was simply to align the positions to multiples of 5 pixel.
    It must also be possible to have one widget placed at scene coordinates, e.g. (10px,10px) and the next one at (15px, 60px) when the dimensions of the widget are e.g. (80px, 40px).

    Maybe, I could use a QGridlayout with a preferred column and row width of 5 px and span my widgets over several rows and columns?

    Rainer

  4. #4
    Join Date
    Jan 2009
    Location
    Czech Republic
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsItem / QGraphicsProxyWidget: how to snap to grid

    I misunderstood you first post then, I'm sorry. I'm also not sure if you can achieve this (if I got you right, you want something like for example grid in gimp where the items can be sticky to the grid) with QGraphicsGridLayout, but I might be wrong.
    But it should be possible to do it with grabbing the pos() of the dropEvent, then round it to the closest 5px and then set the item position to the rounded one (which is - as I understand - what you already tried but with mouseReleaseEvent). If it still does not work for you, post the code.
    Last edited by Palmik; 15th February 2010 at 22:17.

Similar Threads

  1. QGraphicsItem or QGraphicsProxyWidget?
    By ttvo in forum Qt Programming
    Replies: 5
    Last Post: 31st August 2009, 00:13
  2. Replies: 5
    Last Post: 31st January 2009, 08:36
  3. snap to grid behaviour
    By dreamer in forum Qt Programming
    Replies: 1
    Last Post: 3rd May 2008, 09:35
  4. Snap-to grid on QGraphicsItem
    By hardgeus in forum Qt Programming
    Replies: 9
    Last Post: 28th April 2008, 17:22
  5. Snap/Dock QDialog to the edge of the screen
    By durbrak in forum Qt Programming
    Replies: 5
    Last Post: 14th December 2006, 05:41

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.