QT学习-------进度条与定时器关联实现倒计时效果!

很多时候我们需要定时器,定时器QTimer控件,简单实用,那么如何使用呢?

很简单的几部就可以实用Qtimer控件:1.设置定时时间,并开启定时器

     2.超时后的处理。

     3.停止定时器。

第一步实现:    timer->setInterval(1000);
         timer->start();
  或:timer->start(1000);

第二步实现: connect(timer, SIGNAL(timeout()), this, SLOT(Timeout()));


第三步实现:  timer->stop();

示例:定时10秒并与进度条关联

Widget::Widget(QWidget *parent) :
    QWidget(parent)
{

   progressBar = new QProgressBar(this);
   progressBar->setGeometry(29,50,300,21);
   progressBar->setStyleSheet("\
                              QProgressBar::chunk {\
                                  background-color: #CD96CD;\
                                  width: 10px;\
                                  margin: 0.5px;\
                              }");
   progressBar->setRange(0,100);
   progressBar->setValue(100);

   pushButton = new QPushButton(this);
   pushButton->setGeometry(29,100,60,40);
   pushButton->setText("push");

   temp = 100;

   qtimer = new QTimer(this);\

   connect(qtimer, SIGNAL(timeout()), this, SLOT(on_time_out()));
   connect(pushButton,SIGNAL(clicked()),this,SLOT(on_click_button()));

}

Widget::~Widget()
{
}

void Widget::on_time_out()
{
     --temp;
    progressBar->setValue(temp);

    if(temp == 0)
    {
        qtimer->stop();
        temp = 100;
    }
}

void Widget::on_click_button()
{

    if(qtimer->isActive())
    {
        qtimer->stop();
    }
    else
    {
        qtimer->start(100);
    }
}


效果:


示例代码:

https://yunpan.cn/OcPuv95KrX2EUh  访问密码 ab1e

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值