Results 1 to 7 of 7

Thread: help with QGraphicsView

  1. #1
    Join Date
    Nov 2006
    Posts
    29
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default help with QGraphicsView

    If I have a QGraphicsView and a QGraphicsScene set to it, and I want to add a rectangle:

    How can I add it, so it's top left corner will always, no matter how big or small the scene or view is, be in the view's top left corner?

    Thanks,
    Erlend Graff

  2. #2
    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: help with QGraphicsView

    I don't know if I understand the problem correctly but unless you scroll the view an element with position 0,0 will be at the left upper corner of the view.

  3. #3
    Join Date
    Nov 2006
    Posts
    29
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default Re: help with QGraphicsView

    Well, I will try to explain further:

    When I add a rectangle:

    Qt Code:
    1. eventScene.addRect(QRectF(0, 0, 40, 40));
    To copy to clipboard, switch view to plain text mode 

    That would make the rectangle appear centered in the view.

    How can I add it so it would stretch from view coordinates 0,0 to 40,40?

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: help with QGraphicsView

    This is because your scene rect is smaller than your view rect. The scene is centered in the view.

    Maybe you want to override QGraphicsView::drawForeground and make the rectangle always stay at view coordinates ( 0, 0 ).

    Regards

  5. #5
    Join Date
    Nov 2006
    Posts
    29
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default Re: help with QGraphicsView

    How can I do that?

    I tried this:
    Qt Code:
    1. eventView = new QGraphicsView(ui.centralWidget);
    2. eventView->setGeometry(QRect(5, 60, 591, 81));
    3. eventView->setFrameShape(QFrame::Box);
    4. eventView->setFrameShadow(QFrame::Plain);
    5.  
    6. eventScene = new scene;
    7.  
    8. eventView->setScene(eventScene);
    9. eventScene->setSceneRect(0, 0, 480000, 50);
    10.  
    11. QPointF a = eventView->mapToScene(0, 0);
    12.  
    13. eventScene->addRect(QRectF(a.x(), a.y(), 100, 12), QPen(Qt::green), QBrush(Qt::blue));
    14. eventScene->addRect(QRectF(a.x(), a.y()+12, 100, 12), QPen(Qt::red), QBrush(Qt::yellow));
    To copy to clipboard, switch view to plain text mode 

    but when I then scale the whole thing with let's say
    Qt Code:
    1. eventView->scale(2, 2);
    To copy to clipboard, switch view to plain text mode 
    the blue box is cut away, because the top left corner is no longer (0,-14) but (0,0)!

    And that is a problem for me. Any help further is really appreciated!

    Erlend Graff

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: help with QGraphicsView

    Try this:
    Qt Code:
    1. QGraphicsItem* rectItem = eventScene->addRect(QRectF(a.x(), a.y(), 100, 12), QPen(Qt::green), QBrush(Qt::blue));
    To copy to clipboard, switch view to plain text mode 

    Then, later, when you scale the view:
    Qt Code:
    1. QRectF bbox = rectItem->boundingBox();
    2. QPointF itemcenter = eventView->mapToScene( bbox->center() );
    3. eventView->centerOn( itemcenter );
    To copy to clipboard, switch view to plain text mode 

    Remember that is the scene rect is larger than the bounding box of your items, and the items are anchored at (0,0), then they will never appear centered in the view. You will have to modify the scene rect for this, or translate your items.

    Regards

  7. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: help with QGraphicsView

    Also, instead of QGraphicsView::centerOn() you could try to use one of the QGraphicsView::ensureVisible() functions. They all should have the same result.

    Regards

Similar Threads

  1. QGraphicsView, QGraphicsItem, QGraphicsScene
    By Shuchi Agrawal in forum Newbie
    Replies: 10
    Last Post: 23rd March 2011, 20:50
  2. Simulating Video on QLabel or QGraphicsView
    By forrestfsu in forum Qt Programming
    Replies: 9
    Last Post: 21st March 2007, 10:39
  3. How to make MDI editor with QGraphicsView support
    By Kuzemko in forum Qt Programming
    Replies: 4
    Last Post: 14th February 2007, 17:31
  4. Using QGraphicsView with model/view programming
    By JLP in forum Qt Programming
    Replies: 3
    Last Post: 29th January 2007, 11:04
  5. QGraphicsView and QGraphicsScene speeds
    By jnk5y in forum Qt Programming
    Replies: 2
    Last Post: 20th October 2006, 07:13

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.