如何在保持准确绘制位置的情况下优雅地旋转 QBitmap?

huangapple go评论50阅读模式
英文:

How can I rotate the QBitmap nicely while still drawing the position accurately?

问题

我想在任意点上绘制一个 QBitmap

例如,我想在 QBitmap 上绘制一个 点 (5, 5),以匹配屏幕上的 点 (10, 10)

我还想改变围绕该点的 QBitmap 的角度,但如果我旋转它,绘图将失去位置。

如何在保持准确绘制的同时使 QBitmap 旋转得好看?

英文:

I want to draw a QBitmap over an arbitrary point.

For example, I want to draw a point (5, 5) on the QBitmap to match a point (10,10) on the screen.

I also want to change the angle of the QBitmap around that point, but if I rotate it, the drawing will be out of position.

How can I rotate the QBitmap nicely while still drawing the position accurately?

bool MyApp::eventFilter(QObject *obj, QEvent *event)
{
   QBitmap qBmp = QBitmap(".\\pix1.bmp");
   
   int iPixX = 10;
   int iPixY = 10;
   
   int iDrawPointX = 5;
   int iDrawPointY = 5;
   
   if( event->type() == QEvent::Paint ) {
        QPainter painter(m_pUi->drawLayer);

		painter.setPen( QPen(Qt::black) );
		painter.translate(iPixX, iPixY);		
		painter.rotate(45);
		painter.translate(iPixX - iDrawPointX, iPixY - iDrawPointX);

		painter.drawPixmap(0, 0, qBmp);
    }

    painter.end();
    return true;
  }
  
  return false;
 }

答案1

得分: 1

Translate the following code to position your bitmap in screen coordinates:

# 将位图定位到屏幕坐标上:
painter.translate(iPixX - iDrawPointX, iPixY - iDrawPointY);

Make translation to position the bitmap at the point which is the origin of the rotation operation (this is the step you missed; the (5,5) coordinates of the bitmap will be the rotation origin point):

# 进行平移以将位图定位到旋转操作的原点(这是您忽略的步骤,位图的坐标(5,5)将成为旋转的原点):
painter.translate(-iPixX, -iPixY);

Make rotation:

# 进行旋转:
painter.rotate(45);

Back to screen coordinates position:

# 返回到屏幕坐标位置:
painter.translate(iPixX, iPixY);

The full code:

# 完整代码:
painter.translate(iPixX, iPixY);
painter.rotate(45);
painter.translate(-iPixX, -iPixY);
painter.translate(iPixX - iDrawPointX, iPixY - iDrawPointY);
英文:

Make translation to position your bitmap in screen coordinates:

painter.translate(iPixX - iDrawPointX,iPixY - iDrawPointY);

Make translation to position the bitmap in the point which is the origin of rotation operation (this is your missed step, (5,5) coord of bitmap will be rotation origin point):

painter.translate(-iPixX, -iPixY);

Make rotation:

painter.rotate(45);

Back to screen coordinates position:

painter.translate(iPixX, iPixY);    

The full code:

    painter.translate(iPixX, iPixY);        
    painter.rotate(45);
    painter.translate(-iPixX, -iPixY);
    painter.translate(iPixX - iDrawPointX,iPixY - iDrawPointY);

huangapple
  • 本文由 发表于 2023年4月17日 08:04:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030920.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定