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



  • 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();
    

    @



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



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



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



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



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



  • 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! :)



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



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

    Thanks!


Log in to reply
 

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