打印
[嵌入式linux]

4412开发板Qt定时器-实验步骤和部分代码

[复制链接]
1517|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
实验目标:实现计时器功能,并且点击打点按钮将当前时间打印出来。
用到的类有 QTimer 和 QTime,QTimer 是一个计时器类,相当于秒表,QTimer 是一个时间类,相当于手表。
一:实验步骤(迅为4412开发板)
步骤一:界面布局:
拖拽组件,在属性编辑栏设置大小,然后选中按钮,点击水平布局;

在属性编辑栏设置 Label 的最小高度为 50,选中全部组件,点击栅格布局,如图:

根据实际情况调整大小,更改对象名后如下图:

步骤二:创建计时器类对象 timer 和时间类 time,设置初始时间为 0。
  • class TimerP : public QMainWindow
  • {
  • Q_OBJECT
  • public:
  • explicit TimerP(QWidget *parent = 0); ~TimerP();
  • QTimer timer;
  • QTime time;
  • .......... };

[color=rgb(51, 102, 153) !important]复制代码


步骤三:开启计时器对象,设置定时时间,时间到后会发出 timeout() 信号,绑定此信号和自定义的槽函数 timeOut_Slot()。
void start(int msec);
函数功能:开启定时器,时间到后发出 timeout 信号,并重新计时。
参数 msec 含义:定时时间,单位毫秒。
  • TimerP::TimerP(QWidget *parent) :
  • QMainWindow(parent), ui(new Ui::TimerP)
  • {
  • ui->setupUi(this);
  • //信号 timeout 与槽函数绑定
  • connect(&timer,SIGNAL(timeout()),this,SLOT(timeOut_Slot()));
  • time.setHMS(0,0,0,0);
  • ui->showTime->setText("00:00:00:000");
  • }
  • /**开始定时
  • */
  • void TimerP::on_starBu_clicked()
  • {
  • timer.start(3);
  • }

[color=rgb(51, 102, 153) !important]复制代码


步骤四:槽函数 timeOut_Slot()内处理时间类对象,使每次计时时间结束后,时间对象能增加相同的时间,实现计时功能。
QTime addMSecs(int ms) const;
参数 msec 含义:增加的时间值,单位毫秒。
函数功能:返回一个当前时间对象之后 ms 毫秒之后的时间对象。
  • /*
  • * 计时
  • */
  • void TimerP::timeOut_Slot()
  • {
  • //qDebug("timt out");
  • time = time.addMSecs(3);
  • ui->showTime->setText(time.toString("hh:mm:ss.zzz"));
  • }

[color=rgb(51, 102, 153) !important]复制代码


步骤五:打点记录功能,使用全局变量记录排名,并显示到界面。
  • /*
  • * 记录
  • */
  • void TimerP::on_bitBu_clicked()
  • {
  • QString temp;
  • i=i+1;
  • temp.sprintf("%d: ",i);
  • ui->bitTime->append(temp);
  • ui->bitTime->append(time.toString("hh:mm:ss.zzz"));
  • }

[color=rgb(51, 102, 153) !important]复制代码


二:部分代码
  • timerp.h:
  • class TimerP : public QMainWindow
  • {
  • Q_OBJECT
  • public:
  • explicit TimerP(QWidget *parent = 0); ~TimerP();
  • QTimer timer;
  • QTime time;
  • private slots:
  • void on_starBu_clicked();//开始计时按钮槽函数
  • void timeOut_Slot();//定时时间到槽函数
  • void on_closeBu_clicked();//关闭按钮槽函数
  • void on_resetBu_clicked();//重置按钮槽函数
  • void on_bitBu_clicked();//打点记录按钮槽函数
  • private:
  • Ui::TimerP *ui;
  • };
  • timerp.cpp:
  • #include
  • #include
  • static int i;
  • TimerP::TimerP(QWidget *parent) :
  • QMainWindow(parent), ui(new Ui::TimerP)
  • {
  • ui->setupUi(this);
  • connect(&timer,SIGNAL(timeout()),this,SLOT(timeOut_Slot()));
  • time.setHMS(0,0,0,0);
  • ui->showTime->setText("00:00:00:000");
  • }
  • TimerP::~TimerP()
  • {
  • delete ui;
  • }
  • void TimerP::on_starBu_clicked()
  • {
  • timer.start(3);
  • }
  • /*
  • * 处理时间类对象
  • */
  • void TimerP::timeOut_Slot()
  • {
  • //qDebug("timt out");
  • time = time.addMSecs(3);
  • ui->showTime->setText(time.toString("hh:mm:ss.zzz"));
  • }
  • /*
  • * 关闭
  • */
  • void TimerP::on_closeBu_clicked()
  • {
  • timer.stop();
  • i=0;
  • }
  • /*
  • * 重置
  • */
  • void TimerP::on_resetBu_clicked()
  • {
  • timer.stop();
  • time.setHMS(0,0,0,0);
  • ui->showTime->setText("00:00:00:000");
  • ui->bitTime->clear();
  • i=0;
  • }
  • /*
  • * 记录
  • */
  • void TimerP::on_bitBu_clicked()
  • {
  • QString temp;
  • i=i+1;
  • temp.sprintf("%d: ",i);
  • ui->bitTime->append(temp);
  • ui->bitTime->append(time.toString("hh:mm:ss.zzz"));
  • }

[color=rgb(51, 102, 153) !important]复制代码

运行结果

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

634

主题

714

帖子

1

粉丝