2012-01-01 15 views
1

QTを使用すると、ユーザが入力した文字(テキスト)を取り込んで、円のパスに「追従」するようにフォントを描画するにはどうすればよいでしょうか?円を囲むテキストの描画方法は?

+0

あなたはuouが達成したいものをより詳細に説明することはできますか? –

+0

@allenchen、あなたはこのようなことをすることを意味しますか? http://conal.net/pan/Gallery/intro/medres/circle%20text.jpg –

+0

私はあなたの提案に従います。ありがとうございました。すぐに私はここから答えを得ることができます! – allenchen

答えて

2

私は本当にQTについて何も知りませんが、あなたの質問を正しく理解すれば、簡単なGoogle検索で解決策を見つけることができました。コードは以下であり、ここでソースリンクです:

http://developer.qt.nokia.com/faq/answer/how_do_i_make_text_follow_the_line_curve_and_angle_of_the_qpainterpath

#include <QtGui> 
#include <cmath> 

class Widget : public QWidget 
{ 
public: 
    Widget() 
     : QWidget() { } 
private: 
    void paintEvent (QPaintEvent *) 
    { 
     QString hw("hello world"); 
     int drawWidth = width()/100; 
     QPainter painter(this); 
     QPen pen = painter.pen(); 
     pen.setWidth(drawWidth); 
     pen.setColor(Qt::darkGreen); 
     painter.setPen(pen); 

     QPainterPath path(QPointF(0.0, 0.0)); 

     QPointF c1(width()*0.2,height()*0.8); 
     QPointF c2(width()*0.8,height()*0.2); 

     path.cubicTo(c1,c2,QPointF(width(),height())); 

     //draw the bezier curve 
     painter.drawPath(path); 

     //Make the painter ready to draw chars 
     QFont font = painter.font(); 
     font.setPixelSize(drawWidth*2); 
     painter.setFont(font); 
     pen.setColor(Qt::red); 
     painter.setPen(pen); 

     qreal percentIncrease = (qreal) 1/(hw.size()+1); 
     qreal percent = 0; 

     for (int i = 0; i < hw.size(); i++) { 
      percent += percentIncrease; 

      QPointF point = path.pointAtPercent(percent); 
      qreal angle = path.angleAtPercent(percent); 

      qreal rad =qreal(0.017453292519943295769)*angle; // PI/180 

      // From the documentation: 
      /** 
       QTransform transforms a point in the plane to another point using the following formulas: 
       x' = m11*x + m21*y + dx 
       y' = m22*y + m12*x + dy 
      **/ 
      // So the idea is to find the "new position of the character 
      // After we apply the world rotation. 
      // Then translate the painter back to the original position. 
      qreal sina = std::sin(rad); 
      qreal cosa = std::cos(rad); 

      // Finding the delta for the penwidth 
      // Don't divide by 2 because some space would be nice 
      qreal deltaPenX = cosa * pen.width(); 
      qreal deltaPenY = sina * pen.width(); 
      // Finding new posision after rotation 
      qreal newX = (cosa * point.x()) - (sina * point.y()); 
      qreal newY = (cosa * point.y()) + (sina * point.x()); 

      // Getting the delta distance 
      qreal deltaX = newX - point.x(); 
      qreal deltaY = newY - point.y(); 
      // Applying the rotation with the translation. 
      QTransform tran(cosa,sina,-sina,cosa,-deltaX + deltaPenX,-deltaY - deltaPenY); 

      painter.setWorldTransform(tran); 
      painter.drawText(point,QString(hw[i])); 
     } 
    } 

}; 

int main(int argc, char **argv) 
{ 
    QApplication app(argc, argv); 
    Widget widget; 
    widget.show(); 
    return app.exec(); 
} 
+0

ありがとうございました。答えが必要です。 – allenchen

+0

うれしい私は助けることができます。 :) –

関連する問題