0

以下代码在 a 周围绘制文本QPainterPath

QString text = "Short text";
QPainterPath rawPath;
// fill path ...

while (...)
{
    double percent = ...;
    double angle = rawPath.angleAtPercent(percent);
    QPointF point = rawPath.pointAtPercent(percent);

    m_painter.save();

    m_painter.translate(point);
    m_painter.rotate(-angle);

    // Version 1:
    //m_painter.drawText(0, 0, text.at(currentChar));

    // Version 2:
    QPainterPath p2;
    p2.addText(0, 0, font, text.at(currentChar));
    m_painter.drawPath(p2);

    m_painter.restore();
}

图形结果符合预期,但版本 1 和版本 2 的性能都很差。瓶颈是QPainter::drawText()各自的QPainterPath::addText()方法。有没有更有效的方法在路径周围绘制文本?

问候,

4

1 回答 1

1

没有内置方法可以按照路径绘制文本。

但是,如果QPainter效率不够,您可以通过使用文本构建新路径并在循环之后绘制(您不会在循环中使用QPainter::save()and QPainter::restore())来获得一些性能:

void paintEvent(QPaintEvent* event)
    {
        QString text = "Short text";
        text = text.repeated(1000); // Check with a very long text
        QPainterPath rawPath;
        rawPath.addEllipse(QRect(0, 0, 200, 200));

        QPainterPath path;

        double const step = 1.0 / double(text.length());
        double percent = 0.0;
        for(QChar const& c: text)
        {
            double angle = rawPath.angleAtPercent(percent);
            QPointF point = rawPath.pointAtPercent(percent);

            QTransform mat;
            mat.rotate(-angle);
            QPainterPath p2;
            p2.addText(0, 0, font(), c);
            p2 = mat.map(p2);
            path.addPath(p2.translated(point));
            percent += step;
        }

        QPainter painter(this);
        painter.drawPath(path.translated(100, 100));
    }
于 2019-04-02T18:00:00.753 回答