Important: Please read the Qt Code of Conduct -

Loading an image file with QGrapchisScene

  • Hi,

    I imagine that should be something simple to do, but I am failing to do it.

    I want to load an image into a QGraphicsView using a QGraphicsScene, but the image is too big, and I do not know how to make the image fit into the QGraphicsView.

    This is what I am doing:

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

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

    I tried pixMap.scaled(400, 600, Qt::KeepAspectRatio); just after QPixmap pixMap(fileName);, but it made no difference.
    Could anyone, please, tell me what is missing?


  • Lifetime Qt Champion


    QPixmap::scales returns a new object. Did you use the returned QPixmap ?

  • @SGaist

    That was it!!

    Thanks a lot!!!

Log in to reply