温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何使用QT设计秒表功能

发布时间:2022-08-04 16:02:43 来源:亿速云 阅读:193 作者:iii 栏目:开发技术

这篇“如何使用QT设计秒表功能”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“如何使用QT设计秒表功能”文章吧。

设计目标

1. 定时器开始

2.复位从0开始计时

3.记录--把记录的时间添加到QTextBrowser, append(时间)

4. QTime t(0,0,0)   t = t.addMsec( number )  t.toString (“hh:mm:ss:zzz”)

定时器(QTimer)的使用

定时器---定时发送信号timeout
QTimer 定时器类

1.创建定时器类对象
QTimer mtimer;

2.把定时器信号与槽函数关联
connect(&mtimer, &QTimer::timeout, this, &TimerWin::on_outBt_clicked);

3.启动定时器
mtimer.start(1000);

4.停止定时器
mtimer.stop();

QT Creator组件布局

如何使用QT设计秒表功能

运行效果

如何使用QT设计秒表功能

源码

stopwatchwin.h

#ifndef STOPWATCHWIN_H
#define STOPWATCHWIN_H
 
#include <QMainWindow>
#include <QTime>
#include <QTimer>
namespace Ui {
class StopwatchWin;
}
 
class StopwatchWin : public QMainWindow
{
    Q_OBJECT
 
public:
    explicit StopwatchWin(QWidget *parent = nullptr);
    ~StopwatchWin();
    void fun_clicked();
 
private slots:
    void on_pushButton_clicked();
 
    void on_startBt_clicked();
 
    void on_stopBtn_clicked();
 
    void on_recordBtn_clicked();
 
    void on_resertBt_clicked();
 
private:
    Ui::StopwatchWin *ui;
    //QTime t;
    QTime t = QTime(0,0,0,0);
    QTimer mtimer;
};
 
#endif // STOPWATCHWIN_H

stopwatchwin.cpp

#include "stopwatchwin.h"
#include "ui_stopwatchwin.h"
#include <QDebug>
StopwatchWin::StopwatchWin(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::StopwatchWin)
{
    ui->setupUi(this);
    //把定时器信号与槽函数关联
    connect(&mtimer, &QTimer::timeout, this, &StopwatchWin::fun_clicked);
}
 
StopwatchWin::~StopwatchWin()
{
    delete ui;
}
 
void StopwatchWin::fun_clicked()
{
    QString tim = t.toString("hh:mm:ss:zzz");
    t = t.addMSecs(10);
    ui->lcdNumber->display(tim);
    qDebug()<<"1111";
}
void StopwatchWin::on_startBt_clicked()
{
    qDebug()<<"启动定时器";
    mtimer.start(10);
}
 
void StopwatchWin::on_stopBtn_clicked()
{
    qDebug()<<"停止定时器";
    if(mtimer.isActive())
    {
        mtimer.stop();
    }
}
 
void StopwatchWin::on_pushButton_clicked()
{
 
}
 
void StopwatchWin::on_recordBtn_clicked()
{
    QString tim = t.toString("hh:mm:ss:zzz");
    ui->textBrowser->append(tim);
}
 
void StopwatchWin::on_resertBt_clicked()
{
   t = QTime(0,0,0,0);
}

main.cpp

#include "stopwatchwin.h"
#include <QApplication>
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    StopwatchWin w;
    w.show();
 
    return a.exec();
}

以上就是关于“如何使用QT设计秒表功能”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

qt
AI