如何为drawText旋转文本?

时间:2013-07-23 20:29:00

标签: c++ qt qt4 qt5 qpainter

我想将文字旋转45度?

QFont font;
font.setPixelSize(12);

//grid
for(int i = 0; i < 10; i++){
    painter->drawLine(100, 100 + i * 800/9, 900, 100 + i * 800/9);
    str = QString::number((double)9 - i, 'd', 1);
    painter->setFont(font);
    painter->drawText(75, 100 + i * 800/9 - 6, 40, 40, 1, str);
}

3 个答案:

答案 0 :(得分:10)

painter->rotate(45);painter->drawText(75, 100 + i * 800/9 - 6, 40, 40, 1, str);之后插入painter->rotate(-45);(以恢复坐标系的旋转角度):

painter->rotate(45);
painter->drawText(75, 100 + i * 800/9 - 6, 40, 40, 1, str);
painter->rotate(-45);

根据您是指顺时针或逆时针45度,您可能需要否定旋转角度。

旋转坐标系后,您绘制的所有内容都将被旋转,直到您恢复画家为止。保存和恢复画家状态的便捷方法是使用QPainter::save()QPainter::restore()

答案 1 :(得分:6)

painter->save(); // saves current painter state
// painter->rotate(45); clockwise rotation 
// painter->rotate(-45); counter clockwise rotation
painter->restore(); // restores painter state

答案 2 :(得分:4)

为了旋转画家绘制的文本(以及任何其他可绘制对象),只需调用

即可
painter->rotate(yourAngle);

之前

painter->drawText();

如果您希望返回上一个状态,请再次轮换。

painter->rotate(-yourAngle);