欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > (定时器,绘制事件,qt简单服务器的搭建)2025.2.11

(定时器,绘制事件,qt简单服务器的搭建)2025.2.11

2025/2/12 12:47:59 来源:https://blog.csdn.net/CGS200213cgs/article/details/145575172  浏览:    关键词:(定时器,绘制事件,qt简单服务器的搭建)2025.2.11

作业

笔记(复习补充)

1> 制作一个闹钟软件

头文件

#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QPushButton>      //按钮类
#include <QTimer>           //定时器类
#include <QTime>            //时间类
#include <QLineEdit>        //单行文本输入框类
#include <QLabel>           //标签类
#include <QVBoxLayout>      //垂直布局类
#include <QHBoxLayout>      //水平布局类
#include <QDateTime>   // 包含QDateTime类,用于处理日期和时间
#include <QMessageBox> // 包含QMessageBox类,用于显示消息框QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();
private slots:  //槽函数声明void updateTime();  // 更新显示的系统时间void startAlarm();  // 启动闹钟功能void cancelAlarm();  // 取消已设置的闹钟
private:Ui::Widget *ui;//声明指针变量等QLabel *Mylabel;    //标签对象指针,用于显示时间QLineEdit *Myline;  //单行输入框对象,用于输入闹钟时间QPushButton *Mybtn1; //按钮对象,用于启动闹钟(这两也可以只用一个)QPushButton *Mybtn2; //按钮对象,用于关闭闹钟QTimer *Mytimer;       //定时器对象,用于更新系统时间QTimer *mytimer1;      //定时器对象,用于触发对象bool myset;             //用于标记闹钟是否已经设置QVBoxLayout *MymainLayout;QHBoxLayout *MybuttonLayout;
};
#endif // WIDGET_H

源文件

#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);//认父类,实例化this->Mytimer = new QTimer(this);this->mytimer1 = new QTimer(this);this->myset = false;//创建用户界面控件Mylabel = new QLabel("00:00:00",this);  //创建显示时间的标签Myline = new QLineEdit("00:00:00",this);//创建输入时间的文本Mybtn1 = new QPushButton("启动",this);    //创建启动闹钟的按钮Mybtn2 = new QPushButton("取消",this);    //创建取消闹钟的按钮// 设置布局管理器MymainLayout = new QVBoxLayout;  // 创建垂直布局管理器MybuttonLayout = new QHBoxLayout;  // 创建水平布局管理器MybuttonLayout->addWidget(Mybtn1);  // 将启动按钮添加到水平布局MybuttonLayout->addWidget(Mybtn2);  // 将取消按钮添加到水平布局MymainLayout->addWidget(Mylabel);  // 将时间标签添加到垂直布局MymainLayout->addWidget(Myline);  // 将时间输入框添加到垂直布局MymainLayout->addLayout(MybuttonLayout);  // 将水平布局添加到垂直布局// 设置主窗口的布局this->setLayout(MymainLayout);  // 将垂直布局设置为主窗口的布局// 连接信号和槽connect(Mytimer, &QTimer::timeout, this, &Widget::updateTime);  // 连接定时器信号到更新时间槽函数connect(Mybtn1, &QPushButton::clicked, this,&Widget::startAlarm);  // 连接启动按钮信号到启动闹钟槽函数connect(Mybtn2, &QPushButton::clicked, this,&Widget::cancelAlarm);  // 连接取消按钮信号到取消闹钟槽函数// 启动定时器Mytimer->start(1000);  // 设置定时器间隔为1秒}Widget::~Widget()
{delete ui;
}void Widget::updateTime()
{// 更新时间标签显示当前系统时间Mylabel->setText(QDateTime::currentDateTime().toString("hh:mm:ss"));
}void Widget::startAlarm()
{// 获取用户输入的时间并启动闹钟QString inputTime = Myline->text();  // 获取输入框中的时间if (!inputTime.isEmpty()) {QTime targetTime = QTime::fromString(inputTime, "hh:mm:ss");  // 将输入的时间字符串转换为QTime对象QDateTime currentTime = QDateTime::currentDateTime();  // 获取当前时间QDateTime targetDateTime = currentTime.addSecs(targetTime.msecsTo(currentTime.time()));  // 计算目标时间mytimer1->stop();  // 停止之前的定时器(如果有)mytimer1->setInterval(targetDateTime.msecsTo(QDateTime::currentDateTime()));  // 设置定时器间隔connect(mytimer1, &QTimer::timeout, this, [&]() {  // 连接定时器信号到槽函数QMessageBox::information(this, "Alarm", "Time's up!");  // 弹出闹钟提醒myset = false;  // 重置闹钟设置标记mytimer1->stop();  // 停止定时器});mytimer1->start();  // 启动定时器myset = true;  // 设置闹钟设置标记为已设置} else {QMessageBox::warning(this, "Warning", "Please set a valid time.");  // 提示用户输入有效时间}
}void Widget::cancelAlarm()
{// 取消已设置的闹钟if (myset) {Mytimer->stop();  // 停止定时器myset = false;  // 重置闹钟设置标记QMessageBox::information(this, "Alarm Canceled", "Alarm has been canceled.");  // 提示用户闹钟已取消}
}

2> 使用绘制实现,实现时钟(君子作业)

3> 将网络聊天室服务器端,重新实现一遍

4> 思维导图

5> 牛客网上 两个 28

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com