使用QT,我将如何去采取用户提供的输入(文本)和绘制字体的方式,使其“遵循”循环路径?如何画圆周围的文字?
1
A
回答
2
我真的对QT一无所知,但如果我理解你的问题,我发现这个解决方案只需要一个简单的谷歌搜索。代码如下,这里是源链接:
#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 2012-01-01 17:41:55
+0
很高兴我能帮到你。 :) – 2012-01-01 17:42:07
相关问题
- 1. 在圆圈周围绘制文字
- 2. 如何在前景物体周围画一个椭圆
- 3. Raphael.js动画文本周围画圈
- 4. 如何找到围绕另一个圆的圆周旋转的圆的圆周坐标?
- 5. 围绕圆周移动UIImageView?
- 6. 删除报告文字周围的小圆点和标志
- 7. 在Matlab中识别的图像周围画一个圆圈
- 8. 在椭圆形的图画框周围绘制边框
- 9. 周围画中的datagridview
- 10. 如何检测并在眼睛虹膜区域周围画一个圆圈?
- 11. AS2:椭圆周围的吐温
- 12. 对齐圆周围的元素
- 13. 定义数据点周围的椭圆
- 14. 得到一个圆周围的点
- 15. UIBezierPath:如何在带圆角的视图周围添加边框?
- 16. 如何让相机在物体周围的圆圈中移动?
- 17. 提取锚文本周围的文字
- 18. 字段集和图例周围的正确的圆角边框
- 19. 动画文字围绕一个圆圈旋转
- 20. 圆角边框周围IMG犯规秀
- 21. 在点周围绘制椭圆
- 22. 沿圆周围保持正方形
- 23. 使用javascript buggy排列圆周围的圆点
- 24. PegJS:如何在周围的文本
- 25. 如何切换文本周围的线
- 26. Haml的:如何删除文本周围
- 27. CSS - 如何在圆形图像周围添加圆形边框/轮廓?
- 28. 如何在图像周围添加圆角边框?
- 29. 如何在注解周围添加圆作为半径
- 30. 如何删除您想要的字符串周围的文本?
你能在更多的细节什么uou要实现形容? – 2012-01-01 17:15:50
@allenchen,你的意思是做这样的事情吧? http://conal.net/pan/Gallery/intro/medres/circle%20text.jpg – 2012-01-01 17:18:33
我会按照你的建议。谢谢。我能立刻从这里得到答案! – allenchen 2012-01-01 17:44:52