2016-10-28 109 views
0

我正在使用Qt 5.6,我想在圓圈周圍繪製多個文本標籤並旋轉文本標籤以根據圓圈周圍的位置定位文本,因此12點將具有0度的旋轉,3時將被旋轉90度,6時會進行180度旋轉等Qt圍繞其中心點旋轉文本

我對準圍繞其中心位置的文本:

void drawText(QPainter* pobjPainter, qreal fltX, qreal fltY 
        ,int intFlags, const QString* pobjText) { 
     const qreal fltSize = 32767.0; 

     QPointF ptfCorner(fltX, fltY - fltSize); 

     if ((intFlags & Qt::AlignHCenter) == Qt::AlignHCenter) { 
      ptfCorner.rx() -= fltSize/2.0; 
     } else if ((intFlags & Qt::AlignRight) == Qt::AlignRight) { 
      ptfCorner.rx() -= fltSize; 
     } 
     if ((intFlags & Qt::AlignVCenter) == Qt::AlignVCenter) { 
      ptfCorner.ry() += fltSize/2.0; 
     } else if ((intFlags & Qt::AlignTop) == Qt::AlignTop) { 
      ptfCorner.ry() += fltSize; 
     } 
     QRectF rctPos(ptfCorner, QSizeF(fltSize, fltSize)); 
     pobjPainter->drawText(rctPos, intFlags, *pobjText); 
    } 

我想要對文字應用旋轉。

我想重現類似的東西時所顯示的:

http://www.informit.com/articles/article.aspx?p=1405545&seqNum=2

看來,旋轉功能旋轉整個畫家的畫布,所以座標必須考慮到這是真的給旋轉我很難過。我想將文本放在橢圓周圍然後旋轉,我怎麼知道座標應該是什麼?

+0

將在你的情況不是[這個答案](http://stackoverflow.com/a/17820580/1217285)的幫助? – Dmitry

+0

夠搞笑我一直在看這個,但是試過了,它對我來說不起作用....我還在調查中。 – SPlatten

回答

2

與時鐘例如,你可以嘗試類似堅持......

virtual void paintEvent (QPaintEvent *event) override 
    { 
    QPainter painter(this); 
    double radius = std::min(width(), height())/3; 
    for (int i = 0; i < 12; ++i) { 
     int numeral = i + 1; 
     double radians = numeral * 2.0 * 3.141592654/12; 

     /* 
     * Calculate the position of the text centre as it would be required 
     * in the absence of a transform. 
     */ 
     QPoint pos = rect().center() + QPoint(radius * std::sin(radians), -radius * std::cos(radians)); 

     /* 
     * Set up the transform. 
     */ 
     QTransform t; 
     t.translate(pos.x(), pos.y()); 
     t.rotateRadians(radians); 
     painter.setTransform(t); 

     /* 
     * Specify a huge bounding rectangle centred at the origin. The 
     * transform should take care of position and orientation. 
     */ 
     painter.drawText(QRect(-(INT_MAX/2), -(INT_MAX/2), INT_MAX, INT_MAX), Qt::AlignCenter, QString("%1").arg(numeral)); 
    } 
    } 
+0

謝謝,現在我該如何適應0到360這個標籤可能在0到360之間的任何地方? – SPlatten

+0

我不太確定我關注。你知道圓心的座標(我在例子中使用了'rect()。center()'),它的半徑和從垂直角度的順時針角度(根據你的情況0-> 360)。從這些基本參數都應遵循一切。對不起,如果我誤解了。如果你可以更具體,我會很樂意更新答案。 –

+0

謝謝,它的工作原理! – SPlatten