Qt绘制旋转文本

**参考雨田哥-弧形字体:**https://blog.csdn.net/ly305750665/article/details/89645218
效果图
在这里插入图片描述
在这里插入图片描述
头文件

#ifndef QWHROTATETEXT_H
#define QWHROTATETEXT_H

#include <QWidget>
#include <QPaintEvent>
#include <QPainter>
#include <QPropertyAnimation>

class QWHRotateText : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(float m_curAngle READ getAngle WRITE setAngle)
public:
    explicit QWHRotateText(QWidget *parent = nullptr);
    ~QWHRotateText();

    void setText(QString text);
    QString text();
    void setClockWise(bool clockWise);
    bool getClockWise();
    void setPixelSize(int pixelSize);
    int getPixelSize();
    void setMargin(int margin);
    int getMargin();
    void setBgColor(QColor bgColor);
    QColor getBgColor();
    void setTextColor(QColor textColor);
    QColor getTextColor();

    void setStartAngle(qreal startAngle);
    qreal getStartAngle();
    void setLoopTime(int loopTime);
    int getLoopTime();
protected:
    void paintEvent(QPaintEvent *e);
    void drawBg(QPainter *painter);
    void drawText(QPainter *painter);
private:
    double degreeToArc(double degree);
    void setAngle(int curAngle);
    qreal getAngle();
signals:

public slots:
private:
    QString m_text;                     //旋转文本
    bool m_clockWise;                   //顺时针逆时针
    int m_PixelSize;                    //设置文本大小
    int m_margin;                       //文本外边距
    QColor m_bgColor;                   //背景颜色
    QColor m_textColor;                 //文本颜色

    qreal m_startAngle;                 //起始角度
    qreal m_curAngle;                   //当前角度
    int m_loopTime;                     //旋转一周时间,单位:毫秒
    QPropertyAnimation *m_animation;    //动画绘制
};

#endif // QWHROTATETEXT_H

核心代码

void QWHRotateText::paintEvent(QPaintEvent *e)
{
    Q_UNUSED(e);
    int width = this->width();
    int height = this->height();

    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    //绘制背景
    drawBg(&painter);

    painter.translate(width / 2, height / 2);
    painter.rotate(90 - m_startAngle);
    m_clockWise ? (painter.rotate(m_curAngle)) : (painter.rotate(-m_curAngle));

    QFont font;
    font.setPixelSize(m_PixelSize);
    this->setFont(font);

    //绘制文字
    drawText(&painter);
}

void QWHRotateText::drawBg(QPainter *painter)
{
    painter->save();

    painter->setPen(Qt::NoPen);
    painter->setBrush(m_bgColor);
    painter->drawRect(rect());

    painter->restore();
}

void QWHRotateText::drawText(QPainter *painter)
{
    painter->save();

    int width = this->width();
    int height = this->height();
    int side = qMin(width, height);

    int textCount = m_text.count();
    double stepAngle = 360.0 / textCount;

    painter->setPen(m_textColor);
    for (int i = 0; i < textCount; i++)
    {
        int textWidth = fontMetrics().width(m_text.at(i));
        int textHeight = fontMetrics().height();
        int radius = side / 2 - m_margin - textHeight / 2;
        QRect textRect(-textWidth / 2, -(radius + textHeight / 2), textWidth, textHeight + 1);//这里加一个像素,否则文字绘制不全
        painter->drawText(textRect, Qt::AlignCenter, m_text.at(i));

        painter->rotate(stepAngle);

    }

    painter->restore();
}
发布了228 篇原创文章 · 获赞 44 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/qq_40945965/article/details/100629787