Unsolved 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()) { face->setRotation(90); } ui->graphicsView->fitInView(face, Qt::KeepAspectRatio); ui->graphicsView->show(); }
scene
is aQGraphicsScene *
, andface
is aQGraphicsPixmapItem *
.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); glasses->setFlags(QGraphicsItem::ItemIsMovable); // 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 aQGraphicsPixmapItem *
.The idea is that the user can move
glasses
over theface
image.My problem is that I would like to insert
glasses
in a position inscene
, so thatui->graphicsView
would no scroll.Better yet, is it possible to set the
QRectF
ofscene
to a size equals toui->graphicsView->rect()
? I tried to do it, but failed.Thanks!!
-
Hi,
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 thescene
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]( image url)And this is the look after I load
glasses
:
![alt text]( image url)So, what I would like is the
glass
to be positioned somewhere overface
, in order to avoidui->graphicsView
to scroll.Thanks a lot for your time and help!
-
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);
andglasses->setPos(10,10);
butglass
never is positioned overface
.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
-
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 theface
, and another, on top of that, for theglasses
. -
I forgot: change the Z value of glass.