秒表对于我来说并不陌生,在之前自己学习单片机时,实现过秒表和数字钟;基本思路:开启单片机带的定时器,并设置它没10ms溢出一次,分别用三个变量hour,minute,secong记录秒表的时分秒,然后每0.5s刷新一次显示函数,这样最基本的秒表的基本功能就实现了;当然,在Qt里面设计一个秒表也可以用相似的方法就行实现。

由于嵌入式实验要用Qt做个俄罗斯方块游戏,想在游戏中加个模块(游戏的时间);因此才有了设计秒表的想法。在此,想把秒表封装成类模块,提供三个接口:启动秒表,暂停秒表,复位秒表。

1.秒表的界面设计

用QtCreator新建一个QtGUI应用程序,工程名为myStopWatch,类名为MyStopWatch,基类为QWidget。双击mystopwatch.ui文件,进入界面设计的画面,添加三个lineEdit控件,调整大小,并分别命名为lineEditH,lineEditM,textEditS,在font选项下,可以改变字体的大小,完成后的结果如下图:

Qt学习之秒表的实现(StopWatch) (转)-LMLPHP

2.秒表的类实现

新建好的工程里面有四个文件:mystopwatch.h,mystopwatch.cpp,main.cpp,mystopwatch.ui

(1)mystopwatch.h

在public下面添加三个接口函数:

void StartStopwatch();

void ResetStopwatch();

void StopStopwatch();

在private下添加如下代码:

int hourTemp;           //Hour     int minuteTemp;         //Minute     int secondTemp;         //Second     int countTemp;     QTimer *msTimer;     void Display(QString,QString,QString);     void SetStrLength(QString *str, int length);

并设计一个时间槽timeSlot(),每当定时器溢出时,就会执行槽中的代码;

完成后mystopwatch.h文件中的内容如下:

  1. #ifndef MYSTOPWATCH_H
  2. #define MYSTOPWATCH_H
  3. #include <QWidget>
  4. #include<QTimer>
  5. namespace Ui {
  6. class MyStopWatch;
  7. }
  8. class MyStopWatch : public QWidget
  9. {
  10. Q_OBJECT
  11. public:
  12. explicit MyStopWatch(QWidget *parent = 0);
  13. ~MyStopWatch();
  14. void StartStopwatch();  //启动秒表
  15. void ResetStopwatch();  //复位秒表
  16. void StopStopwatch();   //暂停秒表
  17. private:
  18. Ui::MyStopWatch *ui;
  19. int hourTemp;           //Hour
  20. int minuteTemp;         //Minute
  21. int secondTemp;         //Second
  22. int countTemp;
  23. QTimer *msTimer;   //定义一个定时器
  24. void Display(QString,QString,QString);
  25. void SetStrLength(QString *str, int length);
  26. private slots:
  27. void TimeSlot();
  28. };
  29. #endif // MYSTOPWATCH_H
Qt学习之秒表的实现(StopWatch) (转)-LMLPHP
#ifndef MYSTOPWATCH_H
#define MYSTOPWATCH_H #include <QWidget>
#include<QTimer>
namespace Ui {
class MyStopWatch;
} class MyStopWatch : public QWidget
{
Q_OBJECT public:
explicit MyStopWatch(QWidget *parent = 0);
~MyStopWatch(); void StartStopwatch(); //启动秒表
void ResetStopwatch(); //复位秒表
void StopStopwatch(); //暂停秒表 private:
Ui::MyStopWatch *ui; int hourTemp; //Hour
int minuteTemp; //Minute
int secondTemp; //Second
int countTemp; QTimer *msTimer; //定义一个定时器
void Display(QString,QString,QString);
void SetStrLength(QString *str, int length);
private slots:
void TimeSlot();
}; #endif // MYSTOPWATCH_H

(2)mystopwatch.cpp

在类MyStopWatch的构造函数中,对秒表的显示进行初始化,创建一个定时器并把相应的信号与槽进行连接;即在构造函数中添加如下代码:

  1. MyStopWatch::MyStopWatch(QWidget *parent) :
  2. QWidget(parent),
  3. ui(new Ui::MyStopWatch)
  4. {
  5. ui->setupUi(this);
  6. countTemp=0;
  7. secondTemp=0;
  8. minuteTemp=0;
  9. hourTemp=0;
  10. msTimer= new QTimer(this);  //this说明是当前类对象的定时器
  11. //把信号与槽进行连接
  12. connect(msTimer,SIGNAL(timeout()),this,SLOT(TimeSlot()));
  13. }
Qt学习之秒表的实现(StopWatch) (转)-LMLPHP
MyStopWatch::MyStopWatch(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyStopWatch)
{
ui->setupUi(this);
countTemp=0;
secondTemp=0;
minuteTemp=0;
hourTemp=0;
msTimer= new QTimer(this); //this说明是当前类对象的定时器
//把信号与槽进行连接
connect(msTimer,SIGNAL(timeout()),this,SLOT(TimeSlot()));
}

槽TimeSlot()的实现:

  1. void MyStopWatch::TimeSlot()
  2. {
  3. countTemp+=1;
  4. if(countTemp==100)
  5. {
  6. countTemp=0;
  7. secondTemp+=1;
  8. if(secondTemp==60)
  9. {
  10. secondTemp=0;
  11. minuteTemp+=1;
  12. if(minuteTemp==60)
  13. {
  14. minuteTemp=0;
  15. hourTemp+=1;
  16. if(hourTemp==24)
  17. {
  18. hourTemp=0;
  19. }
  20. }
  21. }
  22. }
  23. //把整数转换成字符串
  24. QString hourstr = QString::number(hourTemp);
  25. QString minutestr = QString::number(minuteTemp);
  26. QString secondstr = QString::number(secondTemp);
  27. Display(hourstr,minutestr,secondstr);
  28. }
  29. void MyStopWatch::Display(QString hour, QString minute, QString second)
  30. {
  31. ui->lineEditH->setText(hour);
  32. ui->lineEditM->setText(minute);
  33. ui->lineEditS->setText(second);
  34. }
Qt学习之秒表的实现(StopWatch) (转)-LMLPHP
void MyStopWatch::TimeSlot()
{
countTemp+=1;
if(countTemp==100)
{
countTemp=0;
secondTemp+=1;
if(secondTemp==60)
{
secondTemp=0;
minuteTemp+=1;
if(minuteTemp==60)
{
minuteTemp=0;
hourTemp+=1;
if(hourTemp==24)
{
hourTemp=0;
}
}
}
}
//把整数转换成字符串
QString hourstr = QString::number(hourTemp);
QString minutestr = QString::number(minuteTemp);
QString secondstr = QString::number(secondTemp);
Display(hourstr,minutestr,secondstr);
} void MyStopWatch::Display(QString hour, QString minute, QString second)
{
ui->lineEditH->setText(hour);
ui->lineEditM->setText(minute);
ui->lineEditS->setText(second);
}

启动秒表的代码实现:

  1. void MyStopWatch::StartStopwatch()
  2. {
  3. msTimer->start(10); //10ms
  4. }
Qt学习之秒表的实现(StopWatch) (转)-LMLPHP
void MyStopWatch::StartStopwatch()
{
msTimer->start(10); //10ms
}

此时在main添加一行代码,调用StartStopwatch()来开启秒表,代码如下:

  1. #include <QtGui/QApplication>
  2. #include "mystopwatch.h"
  3. int main(int argc, char *argv[])
  4. {
  5. QApplication a(argc, argv);
  6. MyStopWatch w;
  7. w.StartStopwatch();
  8. w.show();
  9. return a.exec();
  10. }
Qt学习之秒表的实现(StopWatch) (转)-LMLPHP
#include <QtGui/QApplication>
#include "mystopwatch.h" int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyStopWatch w;
w.StartStopwatch();
w.show(); return a.exec();
}

此时,运行程序,我们的秒表就可以跑起来了;其效果图如下,

Qt学习之秒表的实现(StopWatch) (转)-LMLPHP

由图可知我们的秒表的界面有点难看,下面我们对界面进行优化,字体颜色和背景颜色,保证时,分,秒的数字是一位时,对十位进行补零,把十位也显示出来。

3.秒表的界面优化

(1)linetext控件的背景颜色和控件中的字体颜色

在QWidget类对象的属性中,有个palette,点击改变调色板,界面如下:

Qt学习之秒表的实现(StopWatch) (转)-LMLPHP

其中Text可以改变字体的颜色,这里设置为红色;Base是改变控件背景颜色,设置为蓝色。

QPalete::Window,通常指窗口部件的背景色;

QPalette:WindowText,通常指窗口部件的前景色;

QPalette::Base,指文本输入窗口部件(比如QtextEdit,QLinedit等)的背景色.

QPalette::Text,与QPalette::Base一块使用,指文本输入窗口部件的前景色;

QPalette::Button,指按钮窗口部件的背景色;

QPalette::ButtonText,指按钮窗口部件的前景色.

(2)SetStrLength()函数的实现

  1. void MyStopWatch::SetStrLength(QString *str, int length)
  2. {
  3. if(str->length()<length)
  4. {
  5. str->insert(0,"0");
  6. }
  7. }
Qt学习之秒表的实现(StopWatch) (转)-LMLPHP
void MyStopWatch::SetStrLength(QString *str, int length)
{
if(str->length()<length)
{
str->insert(0,"0");
}
}

在槽TimeSlot()中,在调用Display()函数前,添加如下代码:

  1. //把整数转换成字符串
  2. QString hourstr = QString::number(hourTemp);
  3. QString minutestr = QString::number(minuteTemp);
  4. QString secondstr = QString::number(secondTemp);
  5. //设置字符串的长度为2
  6. SetStrLength(&hourstr,2);
  7. SetStrLength(&minutestr,2);
  8. SetStrLength(&secondstr,2);
  9. Display(hourstr,minutestr,secondstr);
Qt学习之秒表的实现(StopWatch) (转)-LMLPHP
 //把整数转换成字符串
QString hourstr = QString::number(hourTemp);
QString minutestr = QString::number(minuteTemp);
QString secondstr = QString::number(secondTemp);
//设置字符串的长度为2
SetStrLength(&hourstr,2);
SetStrLength(&minutestr,2);
SetStrLength(&secondstr,2);
Display(hourstr,minutestr,secondstr);

再次,运行程序,结果如下:

Qt学习之秒表的实现(StopWatch) (转)-LMLPHP

其他接口函数的实现:

  1. void MyStopWatch::ResetStopwatch()
  2. {
  3. ui->lineEditH->setText("00");
  4. ui->lineEditM->setText("00");
  5. ui->lineEditS->setText("00");
  6. countTemp=0;
  7. secondTemp=0;
  8. minuteTemp=0;
  9. hourTemp=0;
  10. }
  11. void MyStopWatch::StopStopwatch()
  12. {
  13. msTimer->stop();
  14. }
Qt学习之秒表的实现(StopWatch) (转)-LMLPHP
void MyStopWatch::ResetStopwatch()
{
ui->lineEditH->setText("00");
ui->lineEditM->setText("00");
ui->lineEditS->setText("00");
countTemp=0;
secondTemp=0;
minuteTemp=0;
hourTemp=0; } void MyStopWatch::StopStopwatch()
{
msTimer->stop();
}

http://blog.csdn.net/lpp0900320123/article/details/26164857

 
04-17 14:59