zoukankan      html  css  js  c++  java
  • Qt学习之秒表的实现(StopWatch) (转)

    秒表对于我来说并不陌生,在之前自己学习单片机时,实现过秒表和数字钟;基本思路:开启单片机带的定时器,并设置它没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文件中的内容如下:

    1. #ifndef MYSTOPWATCH_H  
    2. #define MYSTOPWATCH_H  
    3.   
    4. #include <QWidget>  
    5. #include<QTimer>  
    6. namespace Ui {  
    7.     class MyStopWatch;  
    8. }  
    9.   
    10. class MyStopWatch : public QWidget  
    11. {  
    12.     Q_OBJECT  
    13.   
    14. public:  
    15.     explicit MyStopWatch(QWidget *parent = 0);  
    16.     ~MyStopWatch();  
    17.       
    18.     void StartStopwatch();  //启动秒表  
    19.     void ResetStopwatch();  //复位秒表  
    20.     void StopStopwatch();   //暂停秒表  
    21.   
    22. private:  
    23.     Ui::MyStopWatch *ui;  
    24.       
    25.      int hourTemp;           //Hour  
    26.      int minuteTemp;         //Minute  
    27.      int secondTemp;         //Second  
    28.      int countTemp;  
    29.        
    30.    QTimer *msTimer;   //定义一个定时器  
    31.    void Display(QString,QString,QString);  
    32.    void SetStrLength(QString *str, int length);  
    33. private slots:  
    34.     void TimeSlot();  
    35. };  
    36.   
    37. #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的构造函数中,对秒表的显示进行初始化,创建一个定时器并把相应的信号与槽进行连接;即在构造函数中添加如下代码:

    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. }  
    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.   
    30. void MyStopWatch::Display(QString hour, QString minute, QString second)  
    31. {  
    32.     ui->lineEditH->setText(hour);  
    33.     ui->lineEditM->setText(minute);  
    34.     ui->lineEditS->setText(second);  
    35. }  
    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. }  
    void MyStopWatch::StartStopwatch()
    {
        msTimer->start(10); //10ms
    }

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

    1. #include <QtGui/QApplication>  
    2. #include "mystopwatch.h"  
    3.   
    4. int main(int argc, char *argv[])  
    5. {  
    6.     QApplication a(argc, argv);  
    7.     MyStopWatch w;  
    8.     w.StartStopwatch();  
    9.     w.show();  
    10.   
    11.     return a.exec();  
    12. }  
    #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()函数的实现

    1. void MyStopWatch::SetStrLength(QString *str, int length)  
    2. {  
    3.     if(str->length()<length)  
    4.     {  
    5.         str->insert(0,"0");  
    6.     }  
    7. }  
    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);  
     //把整数转换成字符串
        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);

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

    其他接口函数的实现:

    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. }  
    12.   
    13. void MyStopWatch::StopStopwatch()  
    14. {  
    15.     msTimer->stop();  
    16. }  
    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

     

        

  • 相关阅读:
    Zookeeper系列(二)特征及应用场景
    Scala学习笔记(三)类层级和特质
    zookeeper系列(一)安装
    Scala学习笔记(二)表达式和函数
    Spring笔记(四)SpingAOP
    Spring笔记(三)AOP前篇之动态代理
    Scala学习笔记(一)数据类型
    Linux内核系列设备模型(一) Kobject与Kset
    Spring笔记(二)Core层
    Linux内核系列之Block块层(一)
  • 原文地址:https://www.cnblogs.com/xihong2014/p/6517015.html
Copyright © 2011-2022 走看看