I am trying to rotate a QGraphicsPixmapItem
child. For other QGraphicsItem
, rotation and scaling work fine. But for a QGraphicsPixmapItem
, if the size does not keep aspect ratio, instead of rotation I get shear.
sample code:
#include <QApplication>
#include <QGraphicsView>
#include <QMessageBox>
#include <QGraphicsPixmapItem>
#include <QFileDialog>
int main(int argc, char *argv[])
{
QGraphicsScene s;
s.setSceneRect(-200, -200, 500, 500);
QGraphicsView view(&s);
view.show();
QGraphicsPixmapItem p;
QString fileName = QFileDialog::getOpenFileName(0, QObject::tr("Open Image File"), QString(), QObject::tr("Png files (*.png);;Jpeg files (*.jpg *.jpeg);;Bitmap files (*.bmp)"));
p.setPixmap(QPixmap(fileName));
s.addItem(&p);
QMessageBox::information(0, "", "");
QTransform original = p.transform();
// scale aspect ratio then rotate
QTransform scalingTransform0(0.5, 0, 0, 0, 0.5, 0, 0, 0, 1);
// p.setTransformOriginPoint(p.boundingRect().center()); // doesn't help shear
p.setTransform(scalingTransform0 * original);
p.setRotation(20);
QMessageBox::information(0, "", "");
// scale
QTransform scalingTransform(0.5, 0, 0, 0, 1, 0, 0, 0, 1);
p.setTransform(scalingTransform * original);
QMessageBox::information(0, "", "");
// rotate
p.setRotation(20);
QMessageBox::information(0, "", "");
// unrotate then rotate again
p.setRotation(0);
QMessageBox::information(0, "", "");
QTransform rotTransform = p.transform().rotate(20);
p.setTransform(rotTransform);
// or p.rotate(20);
return app.exec();
}
Result:
I don't know how to get simple rotation, without shearing, for QGraphicsPixmapItem
s, and the item must remember the rotation.