qt 小秒表

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
public slots://cao han shu
    void zyzupdate();

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

private:
    Ui::MainWindow *ui;
    int num;
    int flag;
    QTimer*p;//计时器定在全局里。
};

#endif // MAINWINDOW_H

main.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
#include
#include
#include

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    num=0;
    p=new QTimer;
    connect(p,SIGNAL(timeout()),this,SLOT(zyzupdate()));
}
void MainWindow::zyzupdate()
{
    num++;
    QString temp = QString::number(num,10);
    if(flag==1)
    {
        ui->label->setText(temp);
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    flag=1;
    p->start(1000);
}

void MainWindow::on_pushButton_2_clicked()
{
    if(flag==1)
    {flag=0;}
    else
    {flag=1;}
}

void MainWindow::on_pushButton_3_clicked()
{
    p->stop();
    if(flag==2)
    {
        num=0;
        QString temp = QString::number(num,10);
         ui->label->setText(temp);

    }
    flag=2;

}

你可能感兴趣的:(qt 小秒表)