|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
实验目标:实现计时器功能,并且点击打点按钮将当前时间打印出来。 ?1 U2 J/ ^2 y
用到的类有 QTimer 和 QTime,QTimer 是一个计时器类,相当于秒表,QTimer 是一个时间类,相当于手表。8 H& a. x' K! G8 h
一:实验步骤(迅为4412开发板)5 l1 F4 o! v2 n2 g: X' }. p
步骤一:界面布局:
( g5 Y3 z* {5 S' u( P" d拖拽组件,在属性编辑栏设置大小,然后选中按钮,点击水平布局;: N, ^" l1 I& a5 y' n2 T ]0 B
![]()
; J$ U7 {3 ]; _! [: n ~2 x) M+ F在属性编辑栏设置 Label 的最小高度为 50,选中全部组件,点击栅格布局,如图:0 a6 S6 r, ], ?/ l
![]()
3 S1 _+ J# f( ?6 ~: \! T) M根据实际情况调整大小,更改对象名后如下图:
! n7 {5 e) B' Q$ t7 q j # k$ n: t$ s" |( b' o
步骤二:创建计时器类对象 timer 和时间类 time,设置初始时间为 0。
$ O3 U* a" g. S3 f) Q3 P$ Q& `; J- class TimerP : public QMainWindow
- {
- Q_OBJECT
- public:
- explicit TimerP(QWidget *parent = 0); ~TimerP();
- QTimer timer;
- QTime time;
- .......... };. ^; u X' r' P" Z2 f
8 R' l# J4 `3 z T! [& N
[color=rgb(51, 102, 153) !important]复制代码0 _# C, v5 R7 r f& | T
: }% Q+ i) ?! w8 Z7 c
步骤三:开启计时器对象,设置定时时间,时间到后会发出 timeout() 信号,绑定此信号和自定义的槽函数 timeOut_Slot()。
8 h( ?: w0 @ n# ^) {& E9 r3 Kvoid start(int msec);$ R" x9 ]7 |1 s y7 D# H! ~; i
函数功能:开启定时器,时间到后发出 timeout 信号,并重新计时。7 _# ~0 `$ e7 S2 Q
参数 msec 含义:定时时间,单位毫秒。
- ^0 q8 I6 x' f3 p- 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);
- }
3 I1 p' b; f: e) {2 o7 F ( Z4 N& K" _( N- L" M
[color=rgb(51, 102, 153) !important]复制代码9 \1 M3 e) Y; A
7 A( N# a9 G# E- o
步骤四:槽函数 timeOut_Slot()内处理时间类对象,使每次计时时间结束后,时间对象能增加相同的时间,实现计时功能。* i& h, N0 `' b' c ~. Y
QTime addMSecs(int ms) const;3 V5 S0 ?2 X3 F6 v0 A
参数 msec 含义:增加的时间值,单位毫秒。
- P& d9 z7 o8 Q% ^函数功能:返回一个当前时间对象之后 ms 毫秒之后的时间对象。 k% u x; o; b* s) T
- /*
- * 计时
- */
- void TimerP::timeOut_Slot()
- {
- //qDebug("timt out");
- time = time.addMSecs(3);
- ui->showTime->setText(time.toString("hh:mm:ss.zzz"));
- }0 x2 ]. S. k; u
/ M/ ^, y* O. _, B1 D c[color=rgb(51, 102, 153) !important]复制代码7 n+ c A) P% B, d5 y
6 X" _. S( r# q* r6 t( B
步骤五:打点记录功能,使用全局变量记录排名,并显示到界面。
8 v# Z( _- ^4 P5 C, S1 `- /*
- * 记录
- */
- 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"));
- }( D0 G# C& o+ v7 k& w5 [1 g+ x
4 j9 W6 t- g$ O7 k4 T* z[color=rgb(51, 102, 153) !important]复制代码) r# ?. Q0 L- k% e% z4 o& j
. W2 O. J! M. D6 E; [( q8 \
二:部分代码4 s2 n% D; _' H
- 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"));
- }
0 B9 P# a0 x) P4 f% \
! N+ L: X9 M; ^; u+ d: I: C6 V[color=rgb(51, 102, 153) !important]复制代码' M4 E) {1 a& h3 G' H
9 ~8 a" e' _' i0 G
![]()
* g" M& F$ F9 Z" C3 U |
|