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

devtools/2025/2/12 8:27:31/

作业

笔记(复习补充)

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


http://www.ppmy.cn/devtools/158155.html

相关文章

基于JavaWeb开发的Java+Jsp+SpringMVC漫威手办商城系统设计和实现

基于JavaWeb开发的JavaJspSpringMVC漫威手办商城系统设计和实现 &#x1f345; 作者主页 网顺技术团队 &#x1f345; 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &#x1f4dd; &#x1f345; 文末获取源码联系方式 &#x1f4dd; &#x1f345; 查看下方微信号获取联系方式 承接各种…

Conda命令整理

Conda 是一个功能强大的包和环境管理工具&#xff0c;广泛用于 Python 开发中。除了基本的包和环境管理功能外&#xff0c;Conda 还提供了许多高级用法和技巧&#xff0c;帮助用户更高效地管理和维护 Python 环境。 1. 管理 Conda 本身 命令描述示例conda --version查看 Cond…

大模型基本原理(二)——ChatGPT的工作原理

如何得到一个ChatGPT&#xff1f; 1、无监督预训练&#xff1a;通过大量的文本数据集进行无监督训练&#xff0c;得到一个基座模型&#xff08;只会续写文本&#xff09; 2、监督微调&#xff1a;通过一些人类撰写的高质量对话数据对基座模型进行监督微调&#xff0c;得到一个…

Vue3(1)

一.create-vue // new Vue() 创建一个应用实例 > createApp() // createRouter() createStore() // 将创建实例进行了封装&#xff0c;保证每个实例的独立封闭性import { createApp } from vue import App from ./App.vue// mount 设置挂载点 #app (id为app的盒子) createA…

01docker run

docker run 用于从镜像创建并启动容器。下面是一些常用的选项&#xff1a; -d: 让容器在后台运行&#xff0c;即以守护进程模式运行。--name: 给容器指定一个名称&#xff0c;便于识别和管理。-p: 将宿主机的端口映射到容器内的端口&#xff0c;实现网络通信。-e: 设置环境变量…

SQL-leetcode—1393. 股票的资本损益

1393. 股票的资本损益 Stocks 表&#xff1a; ---------------------- | Column Name | Type | ---------------------- | stock_name | varchar | | operation | enum | | operation_day | int | | price | int | ---------------------- (stock_name, operation_day) 是这张…

C语言基础系列【9】常见存储类型介绍

博主介绍&#xff1a;程序喵大人 35- 资深C/C/Rust/Android/iOS客户端开发10年大厂工作经验嵌入式/人工智能/自动驾驶/音视频/游戏开发入门级选手《C20高级编程》《C23高级编程》等多本书籍著译者更多原创精品文章&#xff0c;首发gzh&#xff0c;见文末&#x1f447;&#x1f…

嵌入式工程师面试经验分享与案例解析

嵌入式工程师岗位受到众多求职者的关注。面试流程严格&#xff0c;技术要求全面&#xff0c;涵盖C/C编程、数据结构与算法、操作系统、嵌入式系统开发、硬件驱动等多个方向。本文将结合真实案例&#xff0c;深入剖析嵌入式工程师的面试流程、常见问题及应对策略&#xff0c;帮助…