Qt实现计时器

一、样图

Qt实现计时器_第1张图片

二、代码

mainwidow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 

class QTimer;
class QTime;

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow

{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void initTime();
    void updateTime();

    void on_button_clicked();

    void on_button2_clicked();

private:
    Ui::MainWindow *ui;
    QTimer *timer;
    QTime *time;
};

#endif // MAINWINDOW_H

mainwindow.cpp


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

#include 
#include 
#include 

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //setFixedSize(288,153);
    timer = new QTimer;
    time = new QTime;

    ui->time->setDigitCount(8);
    initTime();

    connect(timer, &QTimer::timeout, this, &MainWindow::updateTime);
}

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

void MainWindow::initTime() {
    time->setHMS(0 ,0 ,0 );
    ui->time->display(time->toString("hh:mm:ss"));

}

void MainWindow::updateTime() {
    *time = time->addSecs(1);
    ui->time->display(time->toString("hh:mm:ss"));
}

void MainWindow::on_button_clicked()
{
    if (QString::compare(ui->button->text(), "stop")) {
        //不是stop
        timer->start(100);
        ui->button->setText("stop");
    } else {
        //是
        timer->stop();
        ui->button->setText("start");
    }
}


void MainWindow::on_button2_clicked()
{
    if (timer->isActive() == false) {
        initTime();
    }
}

main.cpp


#include "mainwindow.h"

#include 


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

ui_mainwindow.h

/********************************************************************************
** Form generated from reading UI file 'mainwindow.ui'
**
** Created by: Qt User Interface Compiler version 6.5.0
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_MAINWINDOW_H
#define UI_MAINWINDOW_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

QT_BEGIN_NAMESPACE

class Ui_MainWindow
{
public:
    QWidget *centralwidget;
    QWidget *widget;
    QVBoxLayout *verticalLayout_2;
    QLCDNumber *time;
    QHBoxLayout *horizontalLayout;
    QSpacerItem *horizontalSpacer;
    QVBoxLayout *verticalLayout;
    QPushButton *button;
    QPushButton *button2;
    QMenuBar *menubar;
    QStatusBar *statusbar;

    void setupUi(QMainWindow *MainWindow)
    {
        if (MainWindow->objectName().isEmpty())
            MainWindow->setObjectName("MainWindow");
        MainWindow->setEnabled(true);
        MainWindow->resize(352, 187);
        MainWindow->setMaximumSize(QSize(352, 187));
        MainWindow->setMouseTracking(false);
        MainWindow->setAnimated(true);
        centralwidget = new QWidget(MainWindow);
        centralwidget->setObjectName("centralwidget");
        widget = new QWidget(centralwidget);
        widget->setObjectName("widget");
        widget->setGeometry(QRect(0, 0, 351, 151));
        verticalLayout_2 = new QVBoxLayout(widget);
        verticalLayout_2->setObjectName("verticalLayout_2");
        verticalLayout_2->setContentsMargins(0, 0, 0, 0);
        time = new QLCDNumber(widget);
        time->setObjectName("time");

        verticalLayout_2->addWidget(time);

        horizontalLayout = new QHBoxLayout();
        horizontalLayout->setObjectName("horizontalLayout");
        horizontalSpacer = new QSpacerItem(40, 38, QSizePolicy::Expanding, QSizePolicy::Minimum);

        horizontalLayout->addItem(horizontalSpacer);

        verticalLayout = new QVBoxLayout();
        verticalLayout->setObjectName("verticalLayout");
        button = new QPushButton(widget);
        button->setObjectName("button");
        button->setLayoutDirection(Qt::LeftToRight);
        button->setAutoFillBackground(false);

        verticalLayout->addWidget(button);

        button2 = new QPushButton(widget);
        button2->setObjectName("button2");

        verticalLayout->addWidget(button2);


        horizontalLayout->addLayout(verticalLayout);


        verticalLayout_2->addLayout(horizontalLayout);

        MainWindow->setCentralWidget(centralwidget);
        menubar = new QMenuBar(MainWindow);
        menubar->setObjectName("menubar");
        menubar->setGeometry(QRect(0, 0, 352, 22));
        MainWindow->setMenuBar(menubar);
        statusbar = new QStatusBar(MainWindow);
        statusbar->setObjectName("statusbar");
        MainWindow->setStatusBar(statusbar);

        retranslateUi(MainWindow);

        QMetaObject::connectSlotsByName(MainWindow);
    } // setupUi

    void retranslateUi(QMainWindow *MainWindow)
    {
        MainWindow->setWindowTitle(QCoreApplication::translate("MainWindow", "\350\256\241\346\227\266\345\231\250", nullptr));
        button->setText(QCoreApplication::translate("MainWindow", "start", nullptr));
        button2->setText(QCoreApplication::translate("MainWindow", "clear", nullptr));
    } // retranslateUi

};

namespace Ui {
    class MainWindow: public Ui_MainWindow {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_MAINWINDOW_H

你可能感兴趣的:(qt,ui,开发语言,c++)