Set graphic item position when scene changes



  • I want to reposition some items in a QGraphicsScene when the scene size changes, to keep some items along the left and right edges. I haven't figured out a way to do this that doesn't cause stack overflow or other badness. I'd like to be able to do something like this whenever the scene size changes (pseudo-code):

    @void MyScene::setAlignedPositions()
    {
    QList<QGraphicsItem *> list = getAlignedItems();
    foreach (QGraphicsItem * item, list)
    {
    item->setPos(sceneRect().right() - 50, sceneRect().top() - 50);
    }@

    or some reasonable facsimile thereof. Is this even possible?

    Thanks!



  • you'd have to get the transformed viewRect:

    @
    void CMyView::SlotSceneRectChanged(const QRectF & rect)
    {
    // get the viewport rect in scene coordinates
    QRectF visibleRect = mapToScene(viewport().rect());

    // now reposition your items
    QList<QGraphicsItem *> list = scene()->getAlignedItems();

    foreach (QGraphicsItem * item, list)
    {
    item->setPos(visibleRect.right() - 50, visibleRect.top() - 50);
    }
    }
    @



  • this line will throw error as it returns QPolygon

    @QRectF visibleRect = mapToScene(viewport().rect());@

    use

    @ QRectF visibleRect = mapToScene(viewport().rect()).boundingRect();@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.