QT 延时控件

实现效果:效果可能还不够完美,欢迎讨论。

代码下载:https://download.csdn.net/download/gigizhongyan/11068499

 

直接上代码

/*** COPYRIGHT(C)
* @file LableMyForm.h
* @brief  延时圈圈控件
* @author Kiki
* @date   2019-01-15
* @version 1.0
* @note 延时圈圈控件
* History:
*       Date         Version    PR Num   Author     Modification
* 1.    ...            ...       ...       ...         ...
*
*/
#ifndef LABLEMYFORM_H
#define LABLEMYFORM_H
//标准库类
#include <QWidget>
#include <QPainter>
#include <QTimer>
namespace Ui {
class LableMyForm;
}
class LableMyForm : public QWidget
{
    Q_OBJECT
public:
    explicit LableMyForm(QWidget *parent = 0);
    ~LableMyForm();
    ///绘图类
    void paintEvent(QPaintEvent *);//绘图
    ///绘图事件
    QPaintEvent *m_paintevent;
    ///画笔
    QPainter painter;
    ///时间计时器
    QTimer m_timer;
    ///设置最大时间
    void setMaxTime(int Maxtime);
    ///停止
    void stopTime();
private:
    Ui::LableMyForm *ui;
    /// 最大时间
    int m_MaxTime;
    /// 当前时间
    int m_CueentTime;
    /// 秒数弧度因子
    int m ;
public slots:
    ///定时器
    void timeDone();
signals:
    ///设定秒数失败
    void timeover();
    ///设定15秒没有机器刷卡
    void mustCard();
};
 
#endif // LABLEMYFORM_H

CPP文件:

 

//自定义类
#include "lablemyform.h"
#include "ui_lablemyform.h"
 
//标准库类
#include <QDebug>
LableMyForm::LableMyForm(QWidget *parent) :
    QWidget(parent),
    m_MaxTime(30),
    m(0),
    m_CueentTime(0),
    ui(new Ui::LableMyForm)
{
   ui->setupUi(this);
 
   setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);//无边框
   //定时器,用于计时
   connect(&m_timer ,SIGNAL(timeout()), this, SLOT(timeDone()));
}
 
LableMyForm::~LableMyForm()
{
    delete ui;
}
 
//设定最大时间
void LableMyForm::setMaxTime(int Maxtime)
{
 
    if (m_timer.isActive())
    {
        m_timer.stop();
    }
    m = 0;
    m_MaxTime = Maxtime;
    m_CueentTime = Maxtime;
    ui->label->setText(QString::number(m_MaxTime));
    ui->label->setGeometry(22, 18 ,ui->label->width(),ui->label->height());
    m_timer.start(1000);
 
}
 
//画图
void  LableMyForm::paintEvent(QPaintEvent * )
{
 
    //如果是 90 秒(3次加一次)
    //如果是 15 秒(就乘以2)
    if (m_MaxTime == 90)
    {
       if(m_CueentTime % 3 == 0)
          m++;
    }
    else if(m_MaxTime == 15)
    {
         m++;
         m++;
    }
    else
       m++;
    painter.begin(this);
    painter.setPen(QPen(Qt::white, 2));//设置画笔形式
    painter.setRenderHint(QPainter::Antialiasing, true);
    QRect rectbutton(10, 10, 60, 60);
    painter.drawEllipse(rectbutton);//画圆形状按钮==>矩形的内切圆
    // 设置画笔颜色、宽度
    painter.setPen(QPen(QColor(0, 0 , 0), 1));
    painter.setCompositionMode(QPainter::CompositionMode_Clear);
    QRectF rect(10.0, 10.0, 60.0, 60.0);
    // 起始角度
    int startAngle = 360 * 16  ;
    // 跨越度数
    int spanAngle = 10 * 16 * m;
    // 绘制弧线
    painter.drawArc(rect, startAngle, spanAngle);
    // painter.drawEllipse(rectbutton);//画圆形状按钮==>矩形的内切圆
    painter.end();
}
//停止延时控件工作
void LableMyForm::stopTime()
{
  //  m_MaxTime = Maxtime;
   // m_CueentTime = Maxtime;
  // m_CueentTime = 0;
   m = 0;
   m_timer.stop();
}
//定时操作工作
void LableMyForm::timeDone()
{
    update();
    qDebug() << m_MaxTime << m_CueentTime;
    if (m_CueentTime < 0)
         emit timeover();
    if (m_MaxTime == 30)
    {
        if(m_CueentTime <30)
        if(m_CueentTime % 15 == 0)
        emit mustCard();
    }
 
    ui->label->setText(QString::number(m_CueentTime));
    if(m_CueentTime < 10)
    {
        ui->label->setGeometry(30, 18 ,ui->label->width(),ui->label->height());
        //qDebug() << m_CueentTime;
    }
    else
    {
        ui->label->setGeometry(22, 18 ,ui->label->width(),ui->label->height());
    }
    m_CueentTime--;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值