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();
}
  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浮生卍流年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值