网站首页
IC库存
IC展台
电子资讯
技术资料
PDF文档
我的博客
IC72论坛
ic72 logo
资料首页最新产品 技术参数 电路图 设计应用 解决方案 代理商查询 IC替换 IC厂商 电子辞典
关键字: 技术文章 PDF资料 IC价格 电路图 代理商查询 IC替换 IC厂商 电子辞典

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

实验目标:实现计时器功能,并且点击打点按钮将当前时间打印出来。
用到的类有 QTimer 和 QTime,QTimer 是一个计时器类,相当于秒表,QTimer 是一个时间类,相当于手表。
一:实验步骤(迅为4412开发板)
步骤一:界面布局:
拖拽组件,在属性编辑栏设置大小,然后选中按钮,点击水平布局;

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

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

步骤二:创建计时器类对象 timer 和时间类 time,设置初始时间为 0。
  1. class TimerP : public QMainWindow
  2. {
  3. Q_OBJECT
  4. public:
  5. explicit TimerP(QWidget *parent = 0); ~TimerP();
  6. QTimer timer;
  7. QTime time;
  8. .......... };
复制代码

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

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

步骤五:打点记录功能,使用全局变量记录排名,并显示到界面。
  1. /*
  2. * 记录
  3. */
  4. void TimerP::on_bitBu_clicked()
  5. {
  6. QString temp;
  7. i=i+1;
  8. temp.sprintf("%d: ",i);
  9. ui->bitTime->append(temp);
  10. ui->bitTime->append(time.toString("hh:mm:ss.zzz"));
  11. }
复制代码

二:部分代码
  1. timerp.h:
  2. class TimerP : public QMainWindow
  3. {
  4. Q_OBJECT
  5. public:
  6. explicit TimerP(QWidget *parent = 0); ~TimerP();
  7. QTimer timer;
  8. QTime time;
  9. private slots:
  10. void on_starBu_clicked();//开始计时按钮槽函数
  11. void timeOut_Slot();//定时时间到槽函数
  12. void on_closeBu_clicked();//关闭按钮槽函数
  13. void on_resetBu_clicked();//重置按钮槽函数
  14. void on_bitBu_clicked();//打点记录按钮槽函数
  15. private:
  16. Ui::TimerP *ui;
  17. };
  18. timerp.cpp:
  19. #include
  20. #include
  21. static int i;
  22. TimerP::TimerP(QWidget *parent) :
  23. QMainWindow(parent), ui(new Ui::TimerP)
  24. {
  25. ui->setupUi(this);
  26. connect(&timer,SIGNAL(timeout()),this,SLOT(timeOut_Slot()));
  27. time.setHMS(0,0,0,0);
  28. ui->showTime->setText("00:00:00:000");
  29. }
  30. TimerP::~TimerP()
  31. {
  32. delete ui;
  33. }
  34. void TimerP::on_starBu_clicked()
  35. {
  36. timer.start(3);
  37. }
  38. /*
  39. * 处理时间类对象
  40. */
  41. void TimerP::timeOut_Slot()
  42. {
  43. //qDebug("timt out");
  44. time = time.addMSecs(3);
  45. ui->showTime->setText(time.toString("hh:mm:ss.zzz"));
  46. }
  47. /*
  48. * 关闭
  49. */
  50. void TimerP::on_closeBu_clicked()
  51. {
  52. timer.stop();
  53. i=0;
  54. }
  55. /*
  56. * 重置
  57. */
  58. void TimerP::on_resetBu_clicked()
  59. {
  60. timer.stop();
  61. time.setHMS(0,0,0,0);
  62. ui->showTime->setText("00:00:00:000");
  63. ui->bitTime->clear();
  64. i=0;
  65. }
  66. /*
  67. * 记录
  68. */
  69. void TimerP::on_bitBu_clicked()
  70. {
  71. QString temp;
  72. i=i+1;
  73. temp.sprintf("%d: ",i);
  74. ui->bitTime->append(temp);
  75. ui->bitTime->append(time.toString("hh:mm:ss.zzz"));
  76. }
复制代码



热门搜索:PS-415-HG-OEM PDU1215 BQ25895MRTWR TLP1008TEL ULTRABLOK IS-1000 2838319 CC2544RHBR LC1800 PS-615-HG BSV52R 6SPDX-15 PSF2408 2320322 SBB2808-1 UL800CB-15 SBB830-QTY10 02M5000JF SBB1005-1 DRV8313PWPR PS3612 RS1215-RA TLM825GF 602-15 LCR2400
COPYRIGHT:(1998-2010) IC72 达普IC芯片交易网
客户服务:service@IC72.com 库存上载:IC72@IC72.com
(北京)联系方式: 在线QQ咨询:点击这里给我发消息 联系电话:010-82614113 传真:010-82614123
京ICP备06008810号-21 京公网安备 11010802032910 号 企业资质