QT中有两个按钮

编程入门 行业动态 更新时间:2024-10-18 22:25:13

QT<a href=https://www.elefans.com/category/jswz/34/1771373.html style=中有两个按钮"/>

QT中有两个按钮

QT中有两个按钮,开始按钮和停止按钮,按下按钮后开始不断地以二进制的格式不断地往一个文件中写内容,若在按下开始按钮的前20s内按下停止按钮,则写文件这个动作在按下停止按钮过10s后停止。若在按下开始按钮后过20s后才按下停止按钮,则写文件这个动作在按下停止按钮过10s后停止。且文件只保留按下停止按钮的前20s和后10s。

给我在合适的地方增添下述打印,有的打印可能需要循环打印,帮助我debug:

“正在写入数据,距离按下’开始按钮已经经过多少秒”
“按下’停止按钮距离按下’开始按钮’未超过20s”
“按下’停止按钮距离按下’开始按钮’超过20s”
“正在写入数据,距离按下’停止按钮’已经经过多少秒”

经过多少秒的打印是每隔1秒打印一次
请实现我的需求

#include <QApplication>
#include <QPushButton>
#include <QFile>
#include <QTimer>
#include <QDateTime>
#include <QDebug>QFile file; // 创建一个文件对象用于写入数据QDateTime startTime;  // 记录开始按钮按下的时间
QTimer *timer = nullptr;  // 创建一个计时器指针对象bool writingEnabled = false;int main(int argc, char *argv[]) {QApplication app(argc, argv);QWidget mainWidget;mainWidget.setWindowTitle("Binary Writer");QPushButton startButton("开始", &mainWidget);QPushButton stopButton("停止", &mainWidget);startButton.move(50, 50);stopButton.move(150, 50);mainWidget.show();// 当开始按钮按下时的处理函数QObject::connect(&startButton, &QPushButton::clicked, [&]() {if (!writingEnabled) {// 记录开始按钮按下的时间startTime = QDateTime::currentDateTime();qDebug() << "开始按钮按下,开始时间:" << startTime.toString();// 创建文件对象并打开文件file.setFileName("output.bin");if (file.open(QFile::WriteOnly)) {qDebug() << "文件打开成功";writingEnabled = true;// 创建并启动计时器timer = new QTimer;QObject::connect(timer, &QTimer::timeout, [&]() {// 记录当前时间QDateTime currentTime = QDateTime::currentDateTime();int secondsSinceStart = startTime.secsTo(currentTime);qDebug() << "正在写入数据,距离按下'开始按钮'已经经过"<< secondsSinceStart << "秒";// 写入二进制数据QByteArray binaryData;binaryData.append(currentTime.toString("hh:mm:ss.zzz").toUtf8());file.write(binaryData);});timer->start(1000); // 1秒钟触发一次计时器,写入数据} else {qDebug() << "无法打开文件";}}});// 当停止按钮按下时的处理函数QObject::connect(&stopButton, &QPushButton::clicked, [&]() {if (writingEnabled) {// 记录停止按钮按下的时间QDateTime stopTime = QDateTime::currentDateTime();qDebug() << "停止按钮按下,停止时间:" << stopTime.toString();writingEnabled = false;// 关闭计时器if (timer) {timer->stop();delete timer;timer = nullptr;}// 创建并启动用于打印时间差的计时器timer = new QTimer;timer->setInterval(1000);int secondsSinceStart = startTime.secsTo(stopTime);int secondsSinceStop = 0;QObject::connect(timer, &QTimer::timeout, [&]() {qDebug() << "正在写入数据,距离按下'停止按钮'已经经过"<< secondsSinceStop << "秒";secondsSinceStop++;if (secondsSinceStop >= 10) {// 如果经过10秒,关闭计时器timer->stop();delete timer;timer = nullptr;}});timer->start();// 写入从停止时间开始的10秒数据while (stopTime.secsTo(QDateTime::currentDateTime()) < 10) {QByteArray binaryData;binaryData.append(QDateTime::currentDateTime().toString("hh:mm:ss.zzz").toUtf8());file.write(binaryData);}file.close();qDebug() << "文件已关闭";// 判断按下停止按钮的时间差if (secondsSinceStart < 20) {qDebug() << "按下停止按钮距离按下开始按钮未超过20s";} else {qDebug() << "按下停止按钮距离按下开始按钮超过20s";}}});return app.exec();
}
···

更多推荐

QT中有两个按钮

本文发布于:2023-11-16 01:19:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1611102.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中有   按钮   两个   QT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!