6

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?

Flashcap20
  • 105
  • 1
  • 7

1 Answers1

3

Translate the painter to the center of the drawing area (in your case, 1/2 of boundingRect's width/height). Then the rotation will be done relative to the center and the text will not get rotated out of it.

ypnos
  • 50,202
  • 14
  • 95
  • 141
  • Thanks for your answer. Well, logically, I think that should be right too, and when I only translate without rotation, I see that it works fine, (Text is moved from center to bottom right corner) but when I add rotation, the text is still drawn outside. See edits above. – Flashcap20 Jul 30 '16 at 06:40
  • Well, did you try to translate back after the rotation? – ypnos Jul 30 '16 at 10:59
  • Oh.. sorry about that :) I thought painter.restore() will do that for me. One last question. It seems like after the rotation boundingRect is somehow shrunk, so the text is too long for it and half of the last character is out of it. Why is that? – Flashcap20 Jul 30 '16 at 13:44
  • I managed to solve the problem with adding Qt::TextDontClip, but is there a better way to do this? – Flashcap20 Jul 31 '16 at 00:40