Qt绘制倒计时控件

效果图
在这里插入图片描述
头文件

#ifndef QWHTIMIMG_H
#define QWHTIMIMG_H

/*
 * 可设置倒计时秒数
 * 可设置文本颜色
 * 可设置背景填充
 */ 

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

class QWHTimimg : public QWidget
{
    Q_OBJECT
public:
    explicit QWHTimimg(QWidget *parent = nullptr);
    ~QWHTimimg();

    //设置倒计时秒数
    void setSecs(int secs);
    //设置文本颜色
    void setTextColor(const QColor &color);
    //设置背景填充
    void setBgBrush(const QBrush &brush);
    //开启倒计时
    void start(int secs = -1);

    //获取倒计时秒数
    int getSecs()   const;
    //获取文本颜色
    QColor getTextColor()   const;
    //获取背景填充
    QBrush getBgBrush() const;

protected:
    void paintEvent(QPaintEvent *event);

signals:
    void timingFinished();

private slots:
    void onTimeout();

private:
    int m_secs;                         //倒计时总时间(秒)
    int m_curSecs;                      //当前倒计时秒数
    bool m_zoomIn;                      //放大或缩小
    QColor m_textColor;                 //文本颜色
    QBrush m_bgBrush;                   //背景填充
    QTimer *m_timer;                    //定时器控制放大缩小
    QPropertyAnimation *m_animation;    //动画实现放大缩小
};

#endif // QWHTIMIMG_H

cpp文件

 #include "qwhtimimg.h"
#include <QtMath>
#include <QDebug>

QWHTimimg::QWHTimimg(QWidget *parent) : QWidget(parent)
{
    m_secs = 10;            //倒计时总时间
    m_curSecs = m_secs;     //当前倒计时秒数
    m_zoomIn = false;       //放大或缩小
    m_textColor = Qt::blue; //文本颜色
    m_bgBrush = Qt::gray;   //背景填充

    m_animation = new QPropertyAnimation(this, "", this);
    m_animation->setStartValue(100);
    m_animation->setEndValue(300);
    m_animation->setDuration(400);
    connect(m_animation, &QPropertyAnimation::valueChanged, this, [&](const QVariant &value){update();});

    m_timer = new QTimer(this);
    m_timer->setInterval(500);
    connect(m_timer, SIGNAL(timeout()), this, SLOT(onTimeout()));
}

QWHTimimg::~QWHTimimg()
{
    if (m_timer != nullptr)
    {
        delete m_timer;
        m_timer = nullptr;
    }

    if (m_animation != nullptr)
    {
        if (m_animation->state() != QAbstractAnimation::Stopped)
            m_animation->stop();
        delete m_animation;
        m_animation = nullptr;
    }
}

void QWHTimimg::setSecs(int secs)
{
    m_secs = secs;
}

void QWHTimimg::setTextColor(const QColor &color)
{
    m_textColor = color;
}

void QWHTimimg::setBgBrush(const QBrush &brush)
{
    m_bgBrush = brush;
}

void QWHTimimg::start(int secs)
{
    if (secs > 0)
        m_secs = secs;
    m_zoomIn = false;
    m_curSecs = m_secs;
    m_animation->setStartValue(100);
    m_animation->setEndValue(300);
    m_animation->start();
    m_timer->start();
}

int QWHTimimg::getSecs() const
{
    return m_secs;
}

QColor QWHTimimg::getTextColor() const
{
    return m_textColor;
}

QBrush QWHTimimg::getBgBrush() const
{
    return m_bgBrush;
}

void QWHTimimg::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);

    int width = this->width();
    int height = this->height();
    //绘制背景
    painter.setPen(Qt::NoPen);
    painter.setBrush(m_bgBrush);
    painter.drawRect(rect());
    //坐标轴平移
    painter.translate(width / 2, height / 2);
    //绘制文本
    QFont font = painter.font();
    font.setPixelSize(m_animation->currentValue().toFloat() / 100 * (height / 3));
    painter.setFont(font);
    int opacity = 300 - m_animation->currentValue().toInt() + 55;
    m_textColor.setAlpha(opacity);
    painter.setPen(QPen(m_textColor, 2));
    painter.drawText(-width / 2, -height / 2, width, height, Qt::AlignCenter, QString::number(m_curSecs));
}

void QWHTimimg::onTimeout()
{
    m_animation->stop();
    if (m_zoomIn)
    {
        if (m_curSecs == 1)
        {
            m_timer->stop();
            m_curSecs = 0;
            emit timingFinished();
            update();
            return;
        }
        m_animation->setStartValue(100);
        m_animation->setEndValue(300);
        m_curSecs--;
    }
    else
    {
        m_animation->setStartValue(300);
        m_animation->setEndValue(100);
    }

    m_animation->start();
    m_zoomIn = !m_zoomIn;
}
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

浮生卍流年

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

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

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

打赏作者

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

抵扣说明:

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

余额充值