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. How do I resize a QGraphicsView to show the whole QGraphicsScene?
Forum Updated to NodeBB v4.3 + New Features

How do I resize a QGraphicsView to show the whole QGraphicsScene?

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 3 Posters 2.0k Views 2 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
    DL5EU
    wrote on last edited by DL5EU
    #5

    @JonB
    When I start the application, the main window size is 800x600 pixels (current configuration). When I load an image, it might be a screenshot of 1024*600 pixels coming from my spectrum analyser (too big to be shown in an 800x600 window, so scrollbars are displayed) or it might as well have only 512x349 pixels coming from an old oscilloscope (too small to fit in the window, so white space is shown around the image).

    That is the reason why one idea was to resize the window every time a new image is shown. I could as well use "fitInView" and scale the image depending on the current view size. Or just accept what Qt does by default, perhaps not the worst solution. I don't have experience with other applications like this. How would you handle this? The Qt examples often handle only simple cases.

    JonBJ 1 Reply Last reply
    0
    • D DL5EU

      @JonB
      When I start the application, the main window size is 800x600 pixels (current configuration). When I load an image, it might be a screenshot of 1024*600 pixels coming from my spectrum analyser (too big to be shown in an 800x600 window, so scrollbars are displayed) or it might as well have only 512x349 pixels coming from an old oscilloscope (too small to fit in the window, so white space is shown around the image).

      That is the reason why one idea was to resize the window every time a new image is shown. I could as well use "fitInView" and scale the image depending on the current view size. Or just accept what Qt does by default, perhaps not the worst solution. I don't have experience with other applications like this. How would you handle this? The Qt examples often handle only simple cases.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #6

      @DL5EU
      (I think) I would allow the user to resize the main window, rather than my program altering it dynamically at runtime. (If you show your pictures in their own popup window each time that's different and you could size that as you create it for each picture, but that's different.) I would use fitInView() if you are happy shrinking/growing images to fit the viewport, or stick to 1:1 scale and scrollbars if you want to maintain exact resolution. But I am not the greatest UI designer (I regard end-users as irritating but a necessary evil) and I don't know what your application requires, so it's up to you.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Asperamanca
        wrote on last edited by
        #7

        I would also suggest that growing the whole window should, at most, be an option the user can choose. Also, can the user manually resize the window, and if so, would that stop growing it?

        But more generally: From what you describe, I believe you would be better served using QtQuick and QML. It can do all you need in an integrated way, it could automatically grow your windows should you want that (QQuickView::SizeViewToRootObject), it offers GPU-accelerated rendering using different backends (DirectX, OpenGL, Vulcan, Metal) without you needing to worry about it.
        A number of examples are deployed together with Qt, so you can play around a little and see whether it appeals to you.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DL5EU
          wrote on last edited by DL5EU
          #8

          @Asperamanca
          I agree that resizing the main window automatically should be an option that the user can choose.

          However, after some more hours of searching and thinking I have found out that it is in fact very easy to achieve exactly what I want:

          void MainWindow::fitWindow()
          {
              QSize viewSize = ui->graphicsView->viewport()->size();
              resize(size() + (m_image.size() - viewSize));
          }
          

          I have also found out how to scale the displayed image by using QTransform. Not being an experienced Qt programmer makes me spending more time searching for a solution than necessary :-)

          JonBJ 1 Reply Last reply
          0
          • D DL5EU has marked this topic as solved on
          • D DL5EU

            @Asperamanca
            I agree that resizing the main window automatically should be an option that the user can choose.

            However, after some more hours of searching and thinking I have found out that it is in fact very easy to achieve exactly what I want:

            void MainWindow::fitWindow()
            {
                QSize viewSize = ui->graphicsView->viewport()->size();
                resize(size() + (m_image.size() - viewSize));
            }
            

            I have also found out how to scale the displayed image by using QTransform. Not being an experienced Qt programmer makes me spending more time searching for a solution than necessary :-)

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #9

            @DL5EU
            Yes to your fitWindow() code, if it does indeed give what you want.

            QTransform lest you do a variety of transformations, but you have to look up what each m... variable controls. For just scaling note there is QGraphicsItem::setScale(qreal factor), which could be applied to the QGraphicsPixmapItem you should now be using for your image item.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              DL5EU
              wrote on last edited by
              #10

              @JonB
              Thank you for the hint! Scaling only one item is probably what I need if I have more than one graphics object on the scene and I don't want to scale them all. I will try that. As I said: not being an expert... :-)

              JonBJ 1 Reply Last reply
              0
              • D DL5EU

                @JonB
                Thank you for the hint! Scaling only one item is probably what I need if I have more than one graphics object on the scene and I don't want to scale them all. I will try that. As I said: not being an expert... :-)

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #11

                @DL5EU
                There is a QTransfrom for each QGraphicsItem. I think there is one for the scene as a whole, but that's different. Calling a direct QGraphicsItem::setScale(qreal factor) affects only that gfx item. I believe that (as well as dedicated "rotate" or "translate" etc. methods) is just a shortcut/simpler way of manipulating the m... members of the transform, it just addresses the necessary member(s) for you, with a more helpful name.

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  DL5EU
                  wrote on last edited by
                  #12

                  @JonB
                  When I call QGraphicsPixmapItem::setScale(), the item is scaled as expected, but I was not able to find out how I can get the size of the scaled item. boundingRect() remains the same (seems logical as scaling takes place during display).

                  I would like to adjust the scrollbars or adapt the scene size (whatever is necessary) to make the scrollbars appear as soon as a part of the scaled image is invisible (outside of the view/viewport).

                  Do you know a helpful tutorial that explains all this?

                  JonBJ A 2 Replies Last reply
                  0
                  • D DL5EU

                    @JonB
                    When I call QGraphicsPixmapItem::setScale(), the item is scaled as expected, but I was not able to find out how I can get the size of the scaled item. boundingRect() remains the same (seems logical as scaling takes place during display).

                    I would like to adjust the scrollbars or adapt the scene size (whatever is necessary) to make the scrollbars appear as soon as a part of the scaled image is invisible (outside of the view/viewport).

                    Do you know a helpful tutorial that explains all this?

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #13

                    @DL5EU
                    I don't know the answer to your questions. I would play with whatever you are doing to see what is going on. A clear, minimal example of what you are trying/want/doesn't work posted here or possibly on stackoverflow might get you an answer. You could even try in ChatGPT, not that I would normally recommend it! Have a play with QGraphicsView methods like centerOn, ensureVisible() and fitInView(); check how they treat a scaled QGraphicsItem. I would expect that if part of a (scaled or not) image/item it outside of the view/viewport that would cause scrollbars to appear automatically, unless scaled causes a problem.

                    1 Reply Last reply
                    0
                    • D DL5EU

                      @JonB
                      When I call QGraphicsPixmapItem::setScale(), the item is scaled as expected, but I was not able to find out how I can get the size of the scaled item. boundingRect() remains the same (seems logical as scaling takes place during display).

                      I would like to adjust the scrollbars or adapt the scene size (whatever is necessary) to make the scrollbars appear as soon as a part of the scaled image is invisible (outside of the view/viewport).

                      Do you know a helpful tutorial that explains all this?

                      A Offline
                      A Offline
                      Asperamanca
                      wrote on last edited by
                      #14

                      @DL5EU See documentation of QGraphicsItem::setScale:

                      The scale is combined with the item's rotation(), transform() and transformations() to map the item's coordinate system to the parent item.

                      So in order to see the actual size, you can use

                      auto scaledRectSize = item->mapToParent(item->boundingRect()).boundingRect().size();
                      
                      1. Take the item's boundingRect (in item coordinates)
                      2. Map the rectangle to parent coordinates
                      3. Since the result is a QPolygonF (after all, rotations could be involved in the coordinate transformation), we take QPolygonF's boundingRect (since we know no rotation is actually involved)
                      4. Then we take it's size

                      Recommended reading: https://doc.qt.io/qt-6/graphicsview.html#the-graphics-view-coordinate-system

                      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