秒表对于我来说并不陌生,在之前自己学习单片机时,实现过秒表和数字钟;基本思路:开启单片机带的定时器,并设置它没10ms溢出一次,分别用三个变量hour,minute,secong记录秒表的时分秒,然后每0.5s刷新一次显示函数,这样最基本的秒表的基本功能就实现了;当然,在Qt里面设计一个秒表也可以用相似的方法就行实现。
由于嵌入式实验要用Qt做个俄罗斯方块游戏,想在游戏中加个模块(游戏的时间);因此才有了设计秒表的想法。在此,想把秒表封装成类模块,提供三个接口:启动秒表,暂停秒表,复位秒表。
1.秒表的界面设计
用QtCreator新建一个QtGUI应用程序,工程名为myStopWatch,类名为MyStopWatch,基类为QWidget。双击mystopwatch.ui文件,进入界面设计的画面,添加三个lineEdit控件,调整大小,并分别命名为lineEditH,lineEditM,textEditS,在font选项下,可以改变字体的大小,完成后的结果如下图:
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文件中的内容如下:
[cpp] view plain copy print?
- #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
#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的构造函数中,对秒表的显示进行初始化,创建一个定时器并把相应的信号与槽进行连接;即在构造函数中添加如下代码:
[cpp] view plain copy print?
- 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()));
- }
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()的实现:
[cpp] view plain copy print?
- 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);
- }
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); }
启动秒表的代码实现:
[cpp] view plain copy print?
- void MyStopWatch::StartStopwatch()
- {
- msTimer->start(10); //10ms
- }
void MyStopWatch::StartStopwatch() { msTimer->start(10); //10ms }
此时在main添加一行代码,调用StartStopwatch()来开启秒表,代码如下:
[cpp] view plain copy print?
- #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();
- }
#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(); }
此时,运行程序,我们的秒表就可以跑起来了;其效果图如下,
由图可知我们的秒表的界面有点难看,下面我们对界面进行优化,字体颜色和背景颜色,保证时,分,秒的数字是一位时,对十位进行补零,把十位也显示出来。
3.秒表的界面优化
(1)linetext控件的背景颜色和控件中的字体颜色
在QWidget类对象的属性中,有个palette,点击改变调色板,界面如下:
其中Text可以改变字体的颜色,这里设置为红色;Base是改变控件背景颜色,设置为蓝色。
QPalete::Window,通常指窗口部件的背景色;
QPalette:WindowText,通常指窗口部件的前景色;
QPalette::Base,指文本输入窗口部件(比如QtextEdit,QLinedit等)的背景色.
QPalette::Text,与QPalette::Base一块使用,指文本输入窗口部件的前景色;
QPalette::Button,指按钮窗口部件的背景色;
QPalette::ButtonText,指按钮窗口部件的前景色.
(2)SetStrLength()函数的实现
[cpp] view plain copy print?
- void MyStopWatch::SetStrLength(QString *str, int length)
- {
- if(str->length()<length)
- {
- str->insert(0,"0");
- }
- }
void MyStopWatch::SetStrLength(QString *str, int length) { if(str->length()<length) { str->insert(0,"0"); } }
在槽TimeSlot()中,在调用Display()函数前,添加如下代码:
[cpp] view plain copy print?
- //把整数转换成字符串
- 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);
//把整数转换成字符串 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);
再次,运行程序,结果如下:
其他接口函数的实现:
[cpp] view plain copy print?
- 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();
- }
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