[solved] QGraphicsPixmapItem resize using mouse
-
virtual void operator()(QGraphicsItem* item, const QRectF& rect)
{
QGraphicsPixmapItem* imageItem = dynamic_cast<QGraphicsPixmapItem*>(item);
QGraphicsRectItem* rectItem = dynamic_cast<QGraphicsRectItem*>(item);if (imageItem) { QString fileName = "D:/Qt samples/build-QtGraphics-Desktop_Qt_6_3_2_MinGW_64_bit-Debug/google.PNG"; QImage myImage(fileName); QPixmap myPixMap(100, 50); myPixMap = QPixmap::fromImage(myImage); myPixMap = myPixMap.scaled(QSize(rect.width(), rect.height()), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); imageItem->setPixmap(myPixMap); // imageItem->setPos(rect.x(), rect.y()); If i add this line, resize behaviour is unpredictable for operations in which top-left coordinate changes } else if (rectItem) { // Incaseof QGraphicsRectItem, I can set the bounding box and it works fine. rectItem->setRect(rect); }
}
I need to resize QGraphicsPixmapItem using resize handler. It works fine for drag operations in which top-left coordinates of 'QGraphicsPixmapItem'
is not changed (Drag using right side resize handler)It doesn't work correct if drag using top resize handler.
In the above function, 2nd parameter receives the new bounding box size
Original image
Resize using right handle
Resize using top handler
-
It gets solved by adding offset.
QGraphicsPixmapItem* imageItem = dynamic_cast<QGraphicsPixmapItem*>(item);
if (imageItem) { QPixmap myPixMap = sourceImage.scaled(QSize(rect.width(), rect.height()), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); imageItem->setOffset(rect.x(), rect.y()); imageItem->setPixmap(myPixMap); }
-
@Sridharan said in [solved] QGraphicsPixmapItem resize using mouse:
It gets solved by adding offset.
QGraphicsPixmapItem* imageItem = dynamic_cast<QGraphicsPixmapItem*>(item);
if (imageItem)
{
QPixmap myPixMap = sourceImage.scaled(QSize(rect.width(), rect.height()), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
imageItem->setOffset(rect.x(), rect.y());
imageItem->setPixmap(myPixMap);
}can you share the full code bro i have been searching for it for couple of days