Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Image is cut in the right border when scaling in GraphicsView and Scene
Qt 6.12 LTS is out! See what's new in the release blog

Image is cut in the right border when scaling in GraphicsView and Scene

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 4.8k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hi,

    I'm trying to scale a image to fit in the QGraphicView. Sometimes it works, but for example, if I load a image of size 1500x900, the resized image is cut a bit in the right border, even with the scrool activated. What is wrong in the code below?

    @
    int sceneWidth = 800;
    int sceneHeight = 600;

    ui->graphicsView->setFixedWidth(sceneWidth);
    ui->graphicsView->setFixedHeight(sceneHeight);
    

    // mScene is a subclass of QGraphicsScene
    mScene->clear();

    if (!imageQt.isNull()) { // imageQt is QImage
    
        QPixmap originalImage = QPixmap::fromImage(imageQt);
        QPixmap scaledImage = QPixmap(originalImage.scaledToHeight( (int) mScene->height(), Qt::SmoothTransformation) );
    
        ui->graphicsView->setFixedWidth( scaledImage.width() );
        ui->graphicsView->setFixedHeight( scaledImage.height() );
    
        mScene->addPixmap(scaledImage);
        ui->graphicsView->setScene(mScene);
        ui->graphicsView->setSceneRect(scaledImage.rect());
        ui->graphicsView->fitInView(mScene->itemsBoundingRect() , Qt::KeepAspectRatio);
    
        ui->graphicsView->show();
    

    @

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

      What exactly do you expect the value of
      @(int) mScene->height()@
      to be in the above code?

      The clear() you call before should leave the scene rect unchanged, so you'd probably inherit the scene rect from before the clear() call.

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

        For example, for the image of 1645x900, I expected the scaled height to be 600. And calculated the width, maintaning the aspect ratio, to be 1097.

        I'm sorry, but I did not understand what you mean by:
        "The clear() you call before should leave the scene rect unchanged, so you’d probably inherit the scene rect from before the clear() call. "

        I need to call the clear() after setting the scene rect?

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

          What I meant is: I don't see in your code where you set the value of mScene->height(). From that code alone, it could be anything. Still, you base your further code on that value.

          Do you set the scene height somewhere outside the posted code? Have you tried dumping the value of mScene->height() at this point in your code?

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

            Ooops, now I understood. I have set the mScene height in the MainWindow constructor.

            I did not dump the value of mScene->height(). I have tried with @mScene->setSceneRect(QRect())@ before calling my code, but it not worked.

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

              Okay, sorry about that, the issue with mScene->height() distracted me.

              One more thing I notice is this:
              @
              ui->graphicsView->setFixedWidth( scaledImage.width() );
              ui->graphicsView->setFixedHeight( scaledImage.height() );@

              Here, you do not take into account any borders. It might only be a couple of pixels, but it can be enough to cause a scrollbar to appear.

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

                I don't want any white borders. And to ensure that I don't have the scrollbar, I set the scrollbar policy to be always off.

                Now it is working. I have set the width too small for images with big width. That is why the right border of the image was cut.

                So I first calculate the ratio of width and height, and scale on the height or the width, depending of the condition.

                Thanks! :)

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

                  Why not use QPixmap::scaled with Qt::KeepAspectRatio?

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

                    I'm using the QPixmap::scaled, but I will need to calculate the ratio anyway, to set the graphicsview size.

                    Thanks!

                    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
                    • Unsolved