I have a problem placing images on QGraphicsScene
-
I have a set of QImage objects (which are cuts from a larger image) and after some processing I have to place them on a QGraphicsScene at specific positions. No matter what position I specify, the image is always placed in the center.
This is the code I use to display every item (QImage)
void ImageViewer::displayImage(const QImage &image, const QPointF &position, qreal rotationAngle, qreal scaleFactor) { QPixmap pixmap = QPixmap::fromImage(std::move(image)); QPainter painter(&pixmap); QPen pen(Qt::blue, 1); painter.setPen(pen); painter.drawRect(pixmap.rect().adjusted(0, 0, -2, -2)); // the border QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap); // Adjust the position to account for the size of the cropped image QPointF adjustedPosition = position - QPointF(pixmap.width() / 2.0, pixmap.height() / 2.0); item->setPos(adjustedPosition); // <- not working item->setRotation(rotationAngle); item->setScale(scaleFactor); addItem(item); }Any idea on what I did wrong?
-
I have a set of QImage objects (which are cuts from a larger image) and after some processing I have to place them on a QGraphicsScene at specific positions. No matter what position I specify, the image is always placed in the center.
This is the code I use to display every item (QImage)
void ImageViewer::displayImage(const QImage &image, const QPointF &position, qreal rotationAngle, qreal scaleFactor) { QPixmap pixmap = QPixmap::fromImage(std::move(image)); QPainter painter(&pixmap); QPen pen(Qt::blue, 1); painter.setPen(pen); painter.drawRect(pixmap.rect().adjusted(0, 0, -2, -2)); // the border QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap); // Adjust the position to account for the size of the cropped image QPointF adjustedPosition = position - QPointF(pixmap.width() / 2.0, pixmap.height() / 2.0); item->setPos(adjustedPosition); // <- not working item->setRotation(rotationAngle); item->setScale(scaleFactor); addItem(item); }Any idea on what I did wrong?
@franco-amato said in I have a problem placing images on QGraphicsScene:
item->setPos(adjustedPosition); // <- not workingIt is probably working, but you just don't see it.
AFAIK by default, the scene is centering around its first item.
The more items you add the more the scene will expand, but your view is focused on that first item, at whatever position it might be.One solution (there might be other ways) is setting a fixed
sceneRectlike this:QGraphicsView *view; // either set view size or a size of your choice. imageViewer->setSceneRect ( 0, 0, view->width(), view->height() ); -
Not working :( the item is still placed in the center
-
Not working :( the item is still placed in the center
@franco-amato said in I have a problem placing images on QGraphicsScene:
Not working :( the item is still placed in the center
Print the coordinates where you actually set your items to the scene.
What ispostionandadjustedPosition? -
Coordinates are 10, 10
-
Coordinates are 10, 10
-
I am also lost, I though the (0, 0) coordinates are placed at the top-left corner
-
I am also lost, I though the (0, 0) coordinates are placed at the top-left corner
@franco-amato said in I have a problem placing images on QGraphicsScene:
I am also lost, I though the (0, 0) coordinates are placed at the top-left corner
The
QGraphicsViewyes, since it's aQWidget(allQWidgetshave a top-left originated coodinate system).
HoweverQGraphicsItemshave their origin ( 0 / 0 ) at their center point.
So it could be possible that you set the scene position but expect a view port position.Read more here
and here