QT定时器基本用法

在Qt中有很多需要以一定的频率刷新显示的需求,这时就要用到定时器,这个定时器就是在超时的时候发送信号,然后收到信号后做一些处理即可;然后定时器会重启,继续以之前设定的时间,开始倒计时。以下为一个测试代码:


#ifndef TEST_H
#define TEST_H


#include <QWidget>
#include <QLabel>
#include <QTimer>


class Mywidget: public QWidget
{
    Q_OBJECT
public:
    Mywidget(QWidget *parent = 0);
    ~Mywidget();

public slots:
    void RefreshRate();


private:
    QLabel sent_rate_name;
    QLabel recv_rate_name;
    QLabel sent_rate;
    QLabel recv_rate;

    QTimer *mytimer;

    void modulesShow();


};


#endif //




#include "test.h"

//View
Mywidget::Mywidget(QWidget *parent) :
    QWidget(parent)
{
    //set parent
    sent_rate_name.setParent(this);
    sent_rate.setParent(this);
    recv_rate_name.setParent(this);
    recv_rate.setParent(this);


    //set text
    recv_rate_name.setGeometry(QRect(50, 225, 70, 20));
    sent_rate_name.setGeometry(QRect(210, 225, 70, 20));
    recv_rate.setGeometry(QRect(130, 225, 70, 20));
    sent_rate.setGeometry(QRect(300, 225, 70, 20));


    sent_rate_name.setText("上传速度:");
    recv_rate_name.setText("下载速度:");

    mytimer = new QTimer(this);
    connect(mytimer, SIGNAL(timeout()), this, SLOT(RefreshRate())); //定时器处理函数

    mytimer->start(1000);//开始定时,并设置定时时间

    modulesShow();

}

Mywidget::~Mywidget()
{

}

void Mywidget::modulesShow()
{
    sent_rate_name.show();
    recv_rate_name.show();
    sent_rate.show();
    recv_rate.show();
}

void Mywidget::RefreshRate()
{
    sent_rate.setText(format_show(0,global_sent));
    recv_rate.setText(format_show(0,global_rcvd));
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值