QPainterのとQPropertyAnimationテキストアニメーションループで実施される方法(B)を変換して回転します

Aは、レンダリング
ここに画像を挿入説明
2、プロジェクトのアイデアの構成が
翻訳し、指定した角度に回転させ使用して単一のテキスト、個々のテキストの回転方法を描画QPainterの。QPropertyAnimationは、リングのテキスト効果を実現することができます。
第三に、コードスニペット

#ifndef ROUNDTXTWIDGET_H
#define ROUNDTXTWIDGET_H

#include <QObject>
#include <QWidget>
#include <QPainter>
#include <QFontMetrics>
#include <QPropertyAnimation>
class RoundTxtWidget : public QWidget
{
    Q_OBJECT
public:
    RoundTxtWidget(QWidget *parent = nullptr);
    ~RoundTxtWidget();
    void paintEvent(QPaintEvent *event);
    void startAnimation();
private slots:
    void valueChanged_slot(QVariant value);
private:
    int m_startAngle;
    int m_margin;
    QString m_text;
};

#endif // ROUNDTXTWIDGET_H

#include "roundtxtwidget.h"
RoundTxtWidget::RoundTxtWidget(QWidget *parent)
{
    m_text = "abcdefghijklmnopqrstuvwxyz";
    m_margin = 20;
    m_startAngle = 0;
    startAnimation();
}

RoundTxtWidget::~RoundTxtWidget()
{

}
void RoundTxtWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.translate(this->width()/2,this->height()/2);
    painter.rotate(m_startAngle);
    QFont font;
    font.setPixelSize(12);
    painter.setFont(font);
    painter.save();

    int textCount = m_text.count();
    double stepAngle = 360.0 / textCount;
    painter.setPen(QColor("blue"));
    QFontMetrics fontm(font);
    for (int i = 0; i < textCount; i++)
    {
        int textWidth = fontm.width(m_text.at(i));
        int textHeight = fontm.height();
        int radius = qMin(this->width(), this->height()) / 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();
}
void RoundTxtWidget::startAnimation()
{
    QPropertyAnimation *pAnimation = new QPropertyAnimation(this, QByteArray());
    connect(pAnimation, SIGNAL(valueChanged(QVariant)), this, SLOT(valueChanged_slot(QVariant)));
    pAnimation->setStartValue(0);
    pAnimation->setEndValue(360);
    pAnimation->setDuration(6000);
    pAnimation->setLoopCount(-1);
    pAnimation->start();
}
void RoundTxtWidget::valueChanged_slot(QVariant value)
{
    m_startAngle = value.toInt();
    update();
}

IV結論
歓迎展の
QQ:519 096 571
Eメール:[email protected]

公開された30元の記事 ウォンの賞賛1 ビュー1161

おすすめ

転載: blog.csdn.net/u010906468/article/details/102962821