rotated qgraphicssceneitem bounding rect
-
Everything like in the image, i want to get the boundingrect of the rotated item, and since it's a pixmap item, i need to use scene pos as x,y, and pixmap size as width and height, but doesn't really matter since with other items same problems occur, i tried to create a QMatrix, convert it to polygon using map to polygon and then get the bounding rect, but the results are random sh*t popping up on the screen, with incorrect position and size... I am not wasteing my time on randomness since not even 1 thing is correct, and the problem seems very trivial.
I suspect the rect i am using as a source to later transform it into a polygon and apply the same rotation as the item has, is not the same rect before the rotation occurs.
QPolygonF Graphics_Pixmap::rotated_poly() { Polygon p; p.x(scenePos().x()); p.y(scenePos().y()); p.width(m_pixmap_width); p.height(m_pixmap_height); double r = rotation(); p.set_by_rect(QRectF(scenePos().x(), scenePos().y(), m_pixmap_width, m_pixmap_height)); p.angle(rotation()); return *dynamic_cast<QPolygonF*>(&p); }
void Polygon::update_geometry() { QRectF r; r.setRect(0, 0, m_width, m_height); set_by_rect(r); QMatrix matrix; matrix.rotate(m_angle); *dynamic_cast<QPolygonF*>(this) = matrix.map(*dynamic_cast<QPolygonF*>(this)); translate(m_x, m_y); }
void Polygon::set_by_rect(const QRectF& rect) { *dynamic_cast<QPolygonF*>(this) = QTransform().mapToPolygon(rect.toRect()); }
ANd then at the end just use rotated_poly().boundingRect(), but this doesn't work, the results are near random, and it's a very simple thing, notice in another problem i had this approeach worked exactly how it should, so it would generate a rotated polygon.
-
On the right side there is the item without any rotation, after 45 degree rotation on the left side, the blue figure it's the generated polygon which is trying to imitate the scene object rotation, and the yellow figure, it's the final rect generated by the rotated_poly().boundingRect(), but the "coordiantion" if i can name it this was seems wrong, the yellow rect should be same size as the blue one, and the position should be on the center of the item... Idk what to do with it, i tryied many combinations and this one seems "the best", but still not a valid result.
-
Hey, not sure if you found the solution yet. But you can try this:
QGraphicsPixmapItem *pixmapItem = new QGraphicsPixmapItem; pixmapItem->setPixmap(QPixmap(":/images/pixmap.png")); _scene->addItem(pixmapItem); pixmapItem->setRotation(pixmapItem->rotation() + 45.0); QGraphicsRectItem *boundingRect = new QGraphicsRectItem(pixmapItem->sceneBoundingRect()); QPen outlinePen; outlinePen.setWidth(2); outlinePen.setColor(Qt::red); boundingRect->setPen(outlinePen); _scene->addItem(boundingRect);
I believe what you want is the sceneBoundingRect() and not the boundingRect().