[SOLVED]How to fit in view the pixmaps in QGraphicsView/QGraphicsScene without changing aspect ratio



  • I am using QGraphicsView/QGraphicsScene to display an image. The image is always displayed to its original size with scroll bars at the ends. I want QGraphicsView to fit the image automatically as per the the size of the window keeping aspect ratio.
    I tried this but nothing happened:
    @ui->graphicsView->fitInView(0,0,ui->graphicsView->width(),ui->graphicsView->height(),Qt::KeepAspectRatio);@



  • The QGraphicsView scales the view matrix and scrolls the scroll bars to ensure that the scene rectangle rect fits inside the viewport. rect must be inside the scene rect; otherwise, fitInView() cannot guarantee that the whole rect is visible.

    You can try :
    @
    ui->graphicsView->ensureVisible ( scene->sceneRect() );
    ui->graphicsView->fitInView( scene->sceneRect(), Qt::KeepAspectRatio);
    @



  • O thanks! but it works only when screen is maximized, if i change the window size the scroll bars appear again. Perhaps, i have to use this in resize event. We can use this also
    @ui->graphicsView->fitInView(scene->itemsBoundingRect() ,Qt::KeepAspectRatio);@


Log in to reply
 

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