I'm working on a new project in Qt, using QPainter to draw a QWidget. The problem is, when I try to rotate QPainter the text I want to draw rotates out of my QWidget. I know how to solve the problem in general, but somehow I couldn't figure it out until now. I have to translate my QPainter, in order to position my text right for the rotation, but I don't know how to specify that point to where I should translate my coordinate system. My code without the translation:
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();
Could you please explain how to calculate that point I need?
Thanks for your help! :)
Edit:
I edited my code like this:
painter.save();
painter.translate(width/2, height/2);
painter.rotate(textRotation);
painter.drawText(boundingRect, alignmentHorizontal | alignmentVertical, QString(format).arg(progress).arg(rangeMax));
painter.restore();
But it's still rotating out of my drawing area. Any ideas?