Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Resizing pixmaps inside QGraphicsScene when the scene is resized

    General and Desktop
    2
    4
    4798
    Loading More Posts
    • 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.
    • Z
      zurbek last edited by

      Hi there!
      I have a window which looks like this : http://imgur.com/tjhpyOX
      What I want to do is a Game of 15 - I have to put multiple images in a grid inside this QGraphicsScene but the thing is, I want them to resize when I resize this window.
      The widgets inside the window resize automatically as they're inside a QGridLayout but I cant get the images inside the QGraphicsScene to resize.
      How do I do that?

      Thanks in advance!

      1 Reply Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators last edited by

        you could zoom the graphicsview in the resizeEvent. The following code ensures that the image grid (bounding rect) is always visible inside the graphcisview.
        @
        void MyGraphicsView::resizeEvent(QResizeEvent* event)
        {
        QGraphicsView::resizeEvent(event);

         QSize viewportSize = this->viewport().size();
         QSize imageGridSize = ...; //size of all images (bounding rect)
        
         qreal factor = viewportSize.width() / imageGridSize.width();
         if( viewportSize.height() < (imageGridSize * factor).height() )
            factor *= viewSize.height() / (imageSize * factor).height();
        
         this->resetTransform();
         QTransform transform = this->transform();
               transform.scale(factor, factor);
         this->setTransform(transform);
        

        }
        @

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply Reply Quote 0
        • Z
          zurbek last edited by

          Zooming sounds like a good idea, shouldn't I zoom scene according to graphicsview size though?

          1 Reply Last reply Reply Quote 0
          • raven-worx
            raven-worx Moderators last edited by

            this may work in some cases... but resizing to the viewport's size is the correct way since this is the visible area where your items are rendered onto.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply Reply Quote 0
            • First post
              Last post