如何在 QPixmap 中旋转照片?

问题描述

enter image description here

enter image description here

在车速表上标记一个针。

// 이미지 출력
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();

enter image description here

我想要旋转和重新定位。

我该怎么做?

解决方法

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

QGraphicsPixmapItem *pixmap_item = scene.addPixmap(pix);

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

要设置旋转,首先使用 QGraphicsItem::setTransformOriginPoint 设置变换原点,(这将设置您的项目将围绕其旋转的点),然后使用 QGraphicsItem::setRotation 设置旋转:

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

您必须自己设置正确的值,但这应该会引导您找到正确的方法。

,

您可以查看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)); //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,m_pxOriginal);
p->end();
delete p;
m_pxItem->setPixmap(rotatedPixmap);

复制自: Read this forum thread