Draw a text without formulas along any path

Create a path
There is a class QPainterPath
using which you can draw any path using a bunch of methods.
In your case it's possible to use arcTo
.
QPainterPath path;
path.arcTo(rect, 210, -240.0);
Calculate a starting point
There is a little difficulty there: a path has a starting point. If you call arcTo
it will create a line to the beginning of the arc and then draw the arc. So you need to call moveTo
and pass the starting point of the arc as an argument. But where you can get it? Calculate using a formula of an arc? No. Of cource it's possible to experiment and set an approximate point or open a book and find a formula of any curve, but there is an easy way to know a starting point: draw a zero-size curve and get the last point of it:
QPainterPath initialPath;
initialPath.arcTo(rect, 210, 0);
path.moveTo(initialPath.currentPosition());
Draw a text
When you have this path you can get any point of the path using pointAtPercent
and use drawText
to paint a text there.
Here is a code to draw an arc and numbers below:
QPainter p(this);
p.drawArc(rect(), 210 * 16, -240 * 16);
QRectF rect(20, 20, width() - 40, height() - 40);
QPainterPath initialPath;
initialPath.arcTo(rect, 210, 0);
QPainterPath path;
path.moveTo(initialPath.currentPosition());
path.arcTo(rect, 210, -240.0);
for (int i = 0; i <= 10; i += 1)
{
p.drawText(path.pointAtPercent((qreal)i / 10), QString::number(i));
}