2013-07-23 68 views
5

我想旋轉文字45度?如何爲drawText旋轉文本?

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); 
} 

回答

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()

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

旋轉(-45)或保存/恢復可以省略 –

3

爲了旋轉由畫家繪製的文本(以及任何其他可繪製對象)只需要調用

painter->rotate(yourAngle); 

painter->drawText(); 

如果你想恢復到以前的狀態通話旋轉再次。

painter->rotate(-yourAngle);