Important: Please read the Qt Code of Conduct -

positioning image in QGraphicScene

  • Hi!

    In my app there is a QGraphicsView, where I load a image from a file like this:

    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);
      if (face->pixmap().width() > face->pixmap().height()) {
      ui->graphicsView->fitInView(face, Qt::KeepAspectRatio);

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

    Then I load another image:

    void MainWindow::on_btnLoadGlasses_clicked() {
      QString fileName = QFileDialog::getOpenFileName(
          this, tr("Choose glasses"), ".", tr("Image Files (*.png)"));
      QPixmap pixMap(fileName);
      glasses = scene->addPixmap(pixMap);
      // Where to place 'glasses' so that 'scene' does not scroll?
      QRectF sceneRect = scene->sceneRect();
      glasses->setPos(sceneRect.width() / 2, sceneRect.height() / 2);

    glasses is also a QGraphicsPixmapItem *.

    The idea is that the user can move glasses over the face image.

    My problem is that I would like to insert glasses in a position in scene, so that ui->graphicsView would no scroll.

    Better yet, is it possible to set the QRectF of scene to a size equals to ui->graphicsView->rect()? I tried to do it, but failed.


  • Lifetime Qt Champion


    It's the other way around, you change the view rect to match the one of the scene.

    QGraphicsView is a "window" on the scene that can show its full content or a specific area.

  • I see... so how can I position the second image (glass) over the first one (face), without making the scene grows, I mean, becomes larger and/or taller?

    Allow me to illustrate:

    This is how the window looks like when I load face (sorry for the image, but I was using a picture of my son).
    ![alt text](af2e6454-ffa7-4ded-9622-22da5db9affe-image.png image url)

    And this is the look after I load glasses:
    ![alt text](418f8733-83de-4306-a479-48c53debd7ca-image.png image url)

    So, what I would like is the glass to be positioned somewhere over face, in order to avoid ui->graphicsView to scroll.

    Thanks a lot for your time and help!

  • Lifetime Qt Champion

    No worries, I like the fact that you anonymized the image.

    Instinctively, I would use the coordinates of face to position glass.

  • That is what I am trying, but with no success.

    I tried

      QRectF sceneRect = scene->sceneRect();
      glasses->setPos(-sceneRect.width() / 2, 0);

    and some variations of the parameters, like glasses->setPos(sceneRect.width() / 2, 0); and glasses->setPos(10,10);but glass never is positioned over face.

    I am starting to think this is not how it should be done, and that I need to understand better the coordinates system of QGraphicsScene.

    Again, thanks for your help

  • Lifetime Qt Champion

    Why not use face coordinates ?

  • I used glasses->setPos(face->x(), face->y());, and the result was the same. I wonder if I should be using two different layers, one for the face, and another, on top of that, for the glasses.

  • Lifetime Qt Champion

    I forgot: change the Z value of glass.

Log in to reply