Results 1 to 4 of 4

Thread: How to enlarge sceneRect() to the viewport size

  1. #1
    Join Date
    Jul 2007
    Posts
    35
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default How to enlarge sceneRect() to the viewport size

    Hi,
    when you create a QGraphicsView with some items and you move one of them, the others move in the opposite direction.
    This is not very nice, so I'm trying to avoid this enlarging the sceneRect() to the size of the viewport.
    The code should enlarge it if needed, else it should be set to itemsBoundingRect().

    I tried to do it, but when I move an item, it starts jumping around!
    It seems there is an offset problem.
    Anyone else tried to achieve this?

    Here is code:
    Qt Code:
    1. QGraphicsScene *m_drawScene;
    2. QGraphicsView *m_drawView;
    3.  
    4. // ...
    5.  
    6. qreal xBorder = 0.0;
    7. qreal yBorder = 0.0;
    8. QRectF sceneRect = m_drawScene->itemsBoundingRect();
    9. const QWidget *drawArea = m_drawView->viewport();
    10.  
    11. QPoint topLeft = m_drawView->mapFromScene(sceneRect.topLeft());
    12. QPoint bottomRight = m_drawView->mapFromScene(sceneRect.bottomRight());
    13.  
    14. const int windowWidth = drawArea->width();
    15. const int sceneWidth = bottomRight.x() - topLeft.x();
    16. if (sceneWidth < windowWidth) {
    17. xBorder = (windowWidth - sceneWidth) / 2.0;
    18. }
    19.  
    20. const int windowHeight = drawArea->height();
    21. const int sceneHeight = bottomRight.y() - topLeft.y();
    22. if (sceneHeight < windowHeight) {
    23. yBorder = (windowHeight - sceneHeight) / 2.0;
    24. }
    25.  
    26. sceneRect.adjust(-xBorder, -yBorder, xBorder, yBorder);
    27. m_drawScene->setSceneRect(sceneRect);
    To copy to clipboard, switch view to plain text mode 

    Thanks for any help, I really can't find a solution :-(

  2. #2
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to enlarge sceneRect() to the viewport size

    The reason this happens is that you haven't defined a scene rect. QGraphicsScene will assume the scene is as large as the bounding rect of all your items, and QGraphicsView auto-centers the scene in the view. When moving an item, it'll appear that the other items move in the opposite direction until the scene is so large that you need scrollbars. Then, the auto-centering stops.

    Setting a scene rect stops this right away. QGraphicsScene has a scene rect in its constructor, or you can call QGraphicsScene::setSceneRect().
    Last edited by Bitto; 27th August 2007 at 12:15. Reason: Typo
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  3. #3
    Join Date
    Jul 2007
    Posts
    35
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to enlarge sceneRect() to the viewport size

    Thanks for your answer!
    I think to know what is happening, but I don't know how to avoid it.
    You say:
    Quote Originally Posted by Bitto View Post
    When moving an item, it'll appear that the other items move in the opposite direction until the scene is so large that you need scrollbars. Then, the auto-centering stops.
    How to find the needed size so that auto-centering does not happen?
    My idea was to set the scene rect size equal to the viewport size, but probably I'm following the wrong way...
    I'll do some more tries!

  4. #4
    Join Date
    Jul 2007
    Posts
    35
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to enlarge sceneRect() to the viewport size

    Here is another try, but also this one does not work :-(

    Qt Code:
    1. QRectF boundingRect = m_drawScene->itemsBoundingRect();
    2. const QWidget *viewport = m_drawView->viewport();
    3. QRect viewportRect = viewport->geometry();
    4.  
    5. QPoint topLeft = m_drawView->mapFromScene(boundingRect.topLeft());
    6. QPoint bottomRight = m_drawView->mapFromScene(boundingRect.bottomRight());
    7.  
    8. QRect mappedBoundingRect = QRect(topLeft, bottomRight);
    9. QRect sceneRect = viewportRect.united(mappedBoundingRect);
    10. m_drawScene->setSceneRect(sceneRect);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Qt 4.1.1 linker warnings
    By Matt Smith in forum Installation and Deployment
    Replies: 0
    Last Post: 26th February 2006, 22:14

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.