如何在 QPixmap 中旋转照片?

How do I rotate photos in QPixmap?

在速度表上标记了一个指针。

// 이미지 출력
QPixmap pix("C:\Users\WORK002\Desktop\speedmeter.png");
QPixmap pix2("C:\Users\WORK002\Desktop\pointer.png");
QPixmap pointer = pix2.scaled(300, 300);


scene.addPixmap(pix);
scene.addPixmap(pointer);
ui-> graphicsView ->setScene(&scene);
ui-> graphicsView ->show();

我想要旋转和重新定位。

我该怎么做?

你可以看看QPixmap::transformed()

QPixmap QPixmap::transformed(const QTransform &transform, Qt::TransformationMode mode = Qt::FastTransformation) const

规格可以通过QTransform对象给出:

  • rotate()轮换
  • translate() 重新定位

这个可以帮助:

//Please note this method takes 2 mseg to finish for a 20x20 pixmap.
QPixmap rotatedPixmap(m_pixOriginal.size());
rotatedPixmap.fill(QColor::fromRgb(0, 0, 0, 0)); //the new pixmap must be transparent.

QPainter* p = new QPainter(&rotatedPixmap);
QSize size = m_pxOriginal.size();
p->translate(size.height()/2,size.height()/2);
p->rotate(m_iAngle);
p->translate(-size.height()/2,-size.height()/2);
p->drawPixmap(0, 0, m_pxOriginal);
p->end();
delete p;
m_pxItem->setPixmap(rotatedPixmap);

复制自: Read this forum thread

你不必搞乱 QPixmap,你可以操纵 QGraphicsScene::addPixmap 返回的 QGraphicsPixmapItem :

QGraphicsPixmapItem *pixmap_item = scene.addPixmap(pix);

要设置位置,可以使用QGraphicsItem::setPos

要设置旋转,首先设置变换原点 QGraphicsItem::setTransformOriginPoint,(this will set the point around which your item will be rotated) and then set the rotation with QGraphicsItem::setRotation:

pixmap_item->setPos(50, 0);
pixmap_item->setTransformOriginPoint(pixmap_item->boundingRect().center());
pixmap_item->setRotation(90);

您必须自己设置正确的值,但这应该会引导您走正确的路。