当前位置: 首页 > article >正文

(定时器,绘制事件,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_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    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.kler.cn/a/543199.html

相关文章:

  • 在实体机和wsl2中安装docker、使用GPU
  • 正则表达式(竞赛篇)
  • ThinkPHP8视图赋值与渲染
  • ASP.NET Core SignalR的协议协商
  • Vue2生命周期面试题
  • python-leetcode-单词搜索
  • vuedraggable固定某一item的记录
  • 语言大模型基础概念 一(先了解听说过的名词都是什么)
  • 元宵佳节,我的创作纪念日:技术之路的回顾与展望
  • 【多模态大模型】系列1:Transformer Encoder——ViLT、ALBEF、VLMO
  • MVC(Model-View-Controller)framework using Python ,Tkinter and SQLite
  • 机器学习数学基础:22.对称矩阵的对角化
  • LKT4202UGM新一代安全认证加密芯片,守护联网设备和服务安全
  • LeetCode --- 435周赛
  • Java进阶14 TCP日志枚举
  • 如何使用CSS Grid实现两列布局?
  • Tcp_socket
  • 20vue3实战-----使用echarts搭建各种图表
  • xss总结
  • HTTP的状态码
  • C++习题1——24、30—
  • 如何使用DHTMLX Scheduler的拖放功能,在 JS 日程安排日历中创建一组相同的事件
  • C++基础学习记录—this指针
  • uniApp 实现下拉框自定义标签 label 和值 value
  • 【C++】25.封装红黑树实现mymap和myset
  • ANR学习