Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Display image in QGraphicsView after rotating it



  • Hi!

    I loaded a image into a QGraphicsView, and then I rotate it -90º.

    QGraphicsView is not resizing (rescaling) the image, so that it fits the entire QGraphicsView.

    This code laods the image:

    void MainWindow::on_btnLoadFace_clicked() {
      QString fileName = QFileDialog::getOpenFileName(
          this, tr("Open Image"), ".", tr("Image Files (*.png *.jpg *.bmp)"));
    
      QPixmap pixMap(fileName);
      ;
      face = scene->addPixmap(pixMap.scaled(ui->graphicsView->width(),
                                            ui->graphicsView->height(),
                                            Qt::KeepAspectRatio));
      ui->graphicsView->show();
    }
    

    And this rotates the image:

    void MainWindow::on_btnRotateRight_clicked() {
  if (face) {
    face->setRotation(90);
    ui->graphicsView->show();
  }
}
    

    face is a QGraphicsPixmapItem *, and scene is QGraphicsScene *.

    I believe I should handle a signal from QGraphicsView, but I am not sure which.

    Thanks!


  • Lifetime Qt Champion

    Hi,

    Are you looking for QGraphicsView::fitInView ?



  • Thanks for your time!

    I tried this:

    void MainWindow::on_btnRotateRight_clicked() {
      if (face) {
        face->setRotation(90);
        ui->graphicsView->fitInView(0, 0, ui->graphicsView->width(),
                                    ui->graphicsView->height(),
                                    Qt::KeepAspectRatio);
        ui->graphicsView->show();
      }
    }
    

    But it did not work. Am I calling fintInView correctly?

    Thanks!


  • Lifetime Qt Champion

    It would be easier to use the overload I linked, it acts directly on your item.



  • Perfect!! Thanks (again) a lot!


Log in to reply