QPainter 旋转。在哪里翻译?

Fla*_*p20 5 c++ qt qpainter

我正在 Qt 中开发一个新项目,使用 QPainter 绘制 QWidget。问题是,当我尝试旋转 QPainter 时,我想绘制的文本会从 QWidget 中旋转出来。我知道如何解决这个问题,但不知何故,直到现在我才弄明白。我必须翻译我的 QPainter,以便将我的文本定位到正确的旋转位置,但我不知道如何指定该点到我应该翻译我的坐标系的位置。我没有翻译的代码:

QPainter painter;

float width = 40;
float height = 200;

float rangeMin = 0;
float rangeMax = 100;

float progress = 80;
QString format("%1/%2");
int alignmentHorizontal = Qt::AlignHCenter;
int alignmentVertical = Qt::AlignVCenter;

int fontSize = 12;
QColor backgroundColor = Qt::green;
QColor fontColor = Qt::black;

QFont font("Arial", fontSize);
QBrush backgroundBrush(backgroundColor);
QBrush transparentBrush(QColor(0,0,0,0));
QRect boundingRect = QRect(0, 0, width, height);

painter.begin(this);

painter.setFont(font);
painter.setPen(fontColor);

painter.drawRect(boundingRect);

float rectX = 0;
float rectY = 0;
float rectWidth = width;
float rectHeight = (float)height/(qAbs(rangeMin)+rangeMax)*progress;
int textRotation = 90;

painter.setBrush(backgroundBrush);
QRectF rect = QRectF(rectX, rectY, rectWidth, rectHeight);
painter.drawRect(rect);

//This is the text I want to rotate, while keeping it centerd horizontally and vertically in boundingRect.
//painter.translate(x, y);
painter.rotate(textRotation);
painter.drawText(boundingRect, alignmentHorizontal | alignmentVertical, QString(format).arg(progress).arg(rangeMax));

painter.end();
Run Code Online (Sandbox Code Playgroud)

你能解释一下如何计算我需要的那个点吗?

谢谢你的帮助!:)

编辑:

我像这样编辑了我的代码:

painter.save();
painter.translate(width/2, height/2);
painter.rotate(textRotation);
painter.drawText(boundingRect, alignmentHorizontal | alignmentVertical, QString(format).arg(progress).arg(rangeMax));
painter.restore();
Run Code Online (Sandbox Code Playgroud)

但它仍然旋转出我的绘图区域。有任何想法吗?

ypn*_*nos 2

将画家平移到绘图区域的中心(在您的情况下,为boundingRect宽度/高度的1/2)。然后旋转将相对于中心完成,并且文本不会从中心旋转出来。