4

The question is simple ! I want something like this using QPainter class. enter image description here

i dont want values to be rotated. but using QPainter::rotate method I am getting the values like this(rotated format).

enter image description here

Can someone help me to draw the circle with values like the first image using QPainter

NorthCat
  • 9,643
  • 16
  • 47
  • 50
Arun
  • 2,247
  • 3
  • 28
  • 51

2 Answers2

4

Here is the simplest code on how I would do it. Assuming I am painting in the paint event of a widget:

#define PI 3.14159265

[..]

void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    QPoint center = rect().center();
    painter.drawEllipse(center, 2, 2);
    const int radius = 100;
    // Draw semicircle with texts on every 30".
    for (double i = .0; i <= 180.0; i += 30.0) {
        QPoint p(center.x() + radius * cos(i * PI / 180.0),
                 center.y() - radius * sin(i * PI / 180.0));
        painter.drawText(p, QString::number(i));
    }
}
vahancho
  • 20,808
  • 3
  • 47
  • 55
3

Draw a text without formulas along any path

Arc Cubic
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));
    }
Ezee
  • 4,214
  • 1
  • 14
  • 29