Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Set graphic item position when scene changes
Qt 6.11 is out! See what's new in the release blog

Set graphic item position when scene changes

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 4.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    deanc
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • R Offline
      R Offline
      rschaub
      wrote on last edited by
      #2

      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);
      }
      }
      @

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        this line will throw error as it returns QPolygon

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

        use

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved