|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
实验目标:实现计时器功能,并且点击打点按钮将当前时间打印出来。
2 B: v6 T7 R! F" F, ]* M- C V用到的类有 QTimer 和 QTime,QTimer 是一个计时器类,相当于秒表,QTimer 是一个时间类,相当于手表。; S9 M* j' W; I6 \0 y
一:实验步骤(迅为4412开发板)
9 M3 R9 o' j% [, a步骤一:界面布局:
# {9 e% B" h/ }. x0 u拖拽组件,在属性编辑栏设置大小,然后选中按钮,点击水平布局;* L/ `! u1 p3 M2 n1 V
![]()
) L+ p* @: o) L5 G/ m' w2 o3 \# d在属性编辑栏设置 Label 的最小高度为 50,选中全部组件,点击栅格布局,如图:5 y4 E* ]( A7 x% x" Z& Z
![]()
0 u3 m. i& a% v0 u( v" \6 q# G6 I: `根据实际情况调整大小,更改对象名后如下图:: y5 N2 G: w$ a" G" T0 Z4 r1 _# ~* e
![]()
% N1 Q$ v2 P: C1 o步骤二:创建计时器类对象 timer 和时间类 time,设置初始时间为 0。
9 q/ {9 ?% m/ i9 ^& n- m4 \3 {- class TimerP : public QMainWindow
- {
- Q_OBJECT
- public:
- explicit TimerP(QWidget *parent = 0); ~TimerP();
- QTimer timer;
- QTime time;
- .......... };
' h2 U; v4 V8 P/ p
: ]4 R- F! n/ v6 f5 u+ H* ][color=rgb(51, 102, 153) !important]复制代码4 s' ^! G3 h3 |6 D3 G" U; P
+ W1 L- \. Y: w6 Z- F9 S; x步骤三:开启计时器对象,设置定时时间,时间到后会发出 timeout() 信号,绑定此信号和自定义的槽函数 timeOut_Slot()。
+ E6 L4 X. L% o: Qvoid start(int msec);1 _. n4 L- ^- d# q9 H9 z
函数功能:开启定时器,时间到后发出 timeout 信号,并重新计时。 G+ X4 d, `9 J) | Y/ y$ D. n
参数 msec 含义:定时时间,单位毫秒。
9 O) N. v9 I, V- k: Q- 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:
n_starBu_clicked() - {
- timer.start(3);
- }
' ?9 i6 W3 ^0 T; I8 @& e* X ! r f- P# L- [' z
[color=rgb(51, 102, 153) !important]复制代码0 ]: `, z7 l! E, {
4 _0 ?: q# k! z% O3 W z
步骤四:槽函数 timeOut_Slot()内处理时间类对象,使每次计时时间结束后,时间对象能增加相同的时间,实现计时功能。7 J/ F$ f9 }! ~6 L) J1 ~- X
QTime addMSecs(int ms) const;
3 ^% [, b8 @. f参数 msec 含义:增加的时间值,单位毫秒。" l6 ?% G1 P m
函数功能:返回一个当前时间对象之后 ms 毫秒之后的时间对象。
) [( c) U+ x; P3 D8 Y6 v _- /*
- * 计时
- */
- void TimerP::timeOut_Slot()
- {
- //qDebug("timt out");
- time = time.addMSecs(3);
- ui->showTime->setText(time.toString("hh:mm:ss.zzz"));
- }& I2 L* h( o/ n) w6 P1 M8 R9 W
0 s Q8 r& t- g1 Y7 C- U0 q[color=rgb(51, 102, 153) !important]复制代码
' M( M! |6 i0 T2 e& w0 X. p5 P9 ^
步骤五:打点记录功能,使用全局变量记录排名,并显示到界面。. w$ \3 C3 T1 }8 }& i: @
- /*
- * 记录
- */
- void TimerP:
n_bitBu_clicked() - {
- QString temp;
- i=i+1;
- temp.sprintf("%d: ",i);
- ui->bitTime->append(temp);
- ui->bitTime->append(time.toString("hh:mm:ss.zzz"));
- }
. G1 @* x# @: p
0 P' K* P: L \" K[color=rgb(51, 102, 153) !important]复制代码5 g, l0 \9 _% Q% Z& U- V
7 }, ?% d( i4 B: U' d( i二:部分代码& B3 b2 v8 `' v
- 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:
n_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"));
- }
I2 R0 l& _. ^& v& M2 F( m 5 T9 ~* _# L( w; I- Y( ^7 J
[color=rgb(51, 102, 153) !important]复制代码
) R! q5 M% H& d4 D8 l
+ m( }. |+ \. q0 o5 Z. j# Q3 x![]()
% c" |0 j2 T B |
|