Results 1 to 3 of 3

Thread: QGraphicsItem Horizontal/Vertical move only

  1. #1
    Join Date
    Sep 2009
    Posts
    49
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGraphicsItem Horizontal/Vertical move only

    How do you restrict a QGraphicsItem to move horizontally or vertically?
    For example, If an item is at (0,0), how do I restrict the movement between (0,0)-(150,0)?
    Prashant


    qt-sdk-win-opensource-2009.03.1.exe
    Python 2.6.3
    PyQt-Py2.6-gpl-4.6-1
    Win XP, 32 Bit

  2. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsItem Horizontal/Vertical move only

    Quote Originally Posted by prashant View Post
    How do you restrict a QGraphicsItem to move horizontally or vertically?
    For example, If an item is at (0,0), how do I restrict the movement between (0,0)-(150,0)?
    For this you need to subclass QGraphicsScene and re-implement mouseMoveEvent. In mouseMoveEvent, you need to identify that particular item and manually restrict the movable area of that item. Here the point is, the item cannot cannot control this behaviour but its parent can.

  3. #3
    Join Date
    Oct 2007
    Posts
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsItem Horizontal/Vertical move only

    Hello,

    You can also reimplement the item itemChange(GraphicsItemChange change, const QVariant &value) fonction. I use this to implement a snap to grid behaviour.

    This is an example from the doc that keep item inside scene:

    Qt Code:
    1. QVariant Component::itemChange(GraphicsItemChange change, const QVariant &value)
    2. {
    3. if (change == ItemPositionChange && scene()) {
    4. // value is the new position.
    5. QPointF newPos = value.toPointF();
    6. QRectF rect = scene()->sceneRect();
    7. if (!rect.contains(newPos)) {
    8. // Keep the item inside the scene rect.
    9. newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left())));
    10. newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top())));
    11. return newPos;
    12. }
    13. }
    14. return QGraphicsItem::itemChange(change, value);
    15. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 10:31
  2. how to prevent qgraphicsitem repaint while mouse move on it
    By christina123y in forum Qt Programming
    Replies: 22
    Last Post: 17th April 2009, 10:17
  3. Replies: 1
    Last Post: 25th February 2009, 00:34
  4. Move and resize with layout
    By waediowa in forum Qt Programming
    Replies: 0
    Last Post: 14th May 2008, 08:16
  5. Move Rectangle on mouse Move
    By vermarajeev in forum Qt Programming
    Replies: 24
    Last Post: 14th May 2007, 05: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.