Qt 如何使用QPaint类围绕圆编写文本?

Qt 如何使用QPaint类围绕圆编写文本?,qt,Qt,问题很简单!我想要这样的东西。使用QPainter类或使用Qt图形框架: 使用指定的QPainterPath有几种方法可以做到这一点 以下是该页的第二个示例: #include <QtGui> #include <cmath> class Widget : public QWidget { public: Widget () : QWidget() { } private: void paintEvent ( QPaintEvent *)

问题很简单!我想要这样的东西。使用
QPainter
类或使用Qt图形框架


使用指定的
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);   // Clockwise is negative

            painter.save();
            // Move the virtual origin to the point on the curve
            painter.translate(point);
            // Rotate to match the angle of the curve
            // Clockwise is positive so we negate the angle from above
            painter.rotate(-angle);
            // Draw a line width above the origin to move the text above the line
            // and let Qt do the transformations
            painter.drawText(QPoint(0, -pen.width()),QString(hw[i]));
            painter.restore();
        }
    }

};

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    Widget widget;
    widget.show();
    return app.exec();
}
#包括
#包括
类Widget:publicqwidget
{
公众:
小部件()
:QWidget(){}
私人:
无效paintEvent(QPaintEvent*)
{
QString hw(“你好世界”);
int drawWidth=width()/100;
油漆工(本);
QPen pen=painter.pen();
笔设置宽度(drawWidth);
setColor(Qt::暗绿色);
画师:画笔;
QPainterPath路径(QPointF(0.0,0.0));
QPointF c1(宽度()*0.2,高度()*0.8);
QPointF c2(宽度()*0.8,高度()*0.2);
路径立方体(c1,c2,QPointF(宽度(),高度());
//绘制贝塞尔曲线
绘制路径(路径);
//让画家准备好画画
QFont font=painter.font();
font.setPixelSize(drawWidth*2);
painter.setFont(字体);
钢笔颜色(Qt::红色);
画师:画笔;
qreal百分比增加=(qreal)1/(硬件大小()+1);
实际百分比=0;
对于(int i=0;i
这个解决方案很好而且简单,但它不是通用的。它将以错误的顺序从右向左打印文本(希伯来语),并且在双向文本(例如,英语和希伯来语混合的文本)中完全失败。QML中的另一个答案是: