2016-07-29 2 views
2

저는 QP에서 새로운 프로젝트를 작성 중입니다. QPainter를 사용하여 QWidget을 그립니다. 문제는 QPainter를 회전하려고 할 때 내 QWidget에서 회전 할 텍스트가 회전한다는 것입니다. 나는 일반적으로 문제를 해결하는 방법을 알고 있지만, 어쨌든 나는 지금까지 그것을 파악할 수 없었다. 회전을 위해 텍스트를 올바르게 배치하려면 QPainter를 번역해야하지만 좌표계를 변환해야하는 위치를 지정하는 방법을 모르겠습니다. 번역없이 내 코드 :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(); 

당신이 설명해 주시겠습니까 어떻게 내가 필요한 그 시점을 계산?

도움 주셔서 감사합니다. :)

편집 :

는이 같은 내 코드 편집 :

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

을하지만 여전히 내 도면 영역에서 회전합니다. 아이디어가 있으십니까?

답변

1

그리기 영역의 가운데로 painter를 변환합니다 (귀하의 경우, boundingRect의 너비/높이의 1/2). 그런 다음 회전은 중심을 기준으로 수행되고 텍스트는 회전되지 않습니다.

+0

답변 해 주셔서 감사합니다. 글쎄, 논리적으로, 나도 그렇게해야한다고 생각하고, 나는 회전 없이만 번역 할 때, 나는 그것이 잘 작동하는 것을 본다. (텍스트는 가운데에서 오른쪽 하단으로 움직인다.) 그러나 회전을 추가 할 때, 텍스트는 여전히 외부에 그려진다. . 위의 수정 사항을 참조하십시오. – Flashcap20

+0

로테이션 후에 번역을 다시 시도 했습니까? – ypnos

+0

아 .. 미안 해요. 나는 painter.restore()가 나를 위해 그것을 할 것이라고 생각했습니다. 마지막 질문 하나. 회전 boundingRect가 어떻게 든 축소 된 것처럼 보이므로 텍스트가 너무 길어 마지막 문자의 절반이 벗어납니다. 왜 그런가요? – Flashcap20