2月11日QT

1> 制作一个闹钟软件

头文件:

复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTimer>  //定时器类
#include <QTime>   //时间类
#include <QTimerEvent> //定时器事件类
#include <QDateTime>   //日期时间类
#include <QDebug>
#include <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 funTime();            //定时器的槽函数
    void on_startBtn_clicked();//启动按键的槽函数
    void on_cancleBtn_clicked();//取消按键的槽函数

private:
    Ui::Widget *ui;

    QTimer *timer;    //定时器指针
    QTime alarmTime;  //时间类对象
    bool alarmSet=false;  //开启或关闭闹钟条件之一(按键决定)
};
#endif // WIDGET_H

源文件:

复制代码
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //给定时器实例化空间
    timer= new(QTimer);
    //连接定时器作用的槽函数(发送者,发送的信号,接收者,处理信号的槽函数)
    connect(timer, &QTimer::timeout, this, &Widget::funTime);
    //开启定时器
    timer->start(1000);   //每1秒发送一次信号
}

Widget::~Widget()
{
    delete ui;
}

//定时器的槽函数
void Widget::funTime()
{
    //显示当前时间
    QTime nowtime=QTime::currentTime();//获取当前时间
    QString t=nowtime.toString("hh:mm:ss");//转换成字符串类型
    ui->timeLab->setText(t);      //显示到Lab

    //闹钟
    if(alarmSet && nowtime>=alarmTime)
    {
        // 获取QTextEdit中的内容
        QString text=ui->textEdit->toPlainText();
        qDebug()<<text;
        //闹钟对话框
        QMessageBox::information(this,"闹钟","起床当牛马啦!");
        alarmSet=false;   //闹钟条件改为 否
    }
}

//启动按键的槽函数
void Widget::on_startBtn_clicked()
{
    //获取输入时间
    QString timeString=ui->timeEdit->text();
    QTime time=QTime::fromString(timeString,"hh:mm:ss");//转换成时间类型
    //判断输入的时间是否合法
    if (time.isValid())
    {
        alarmTime = time;   //将输入时间赋给闹钟时间
        alarmSet = true;    //闹钟条件改为 是
    } else
    {
        QMessageBox::warning(this, "错误", "请输入有效的时间格式 (hh:mm:ss)");
    }
}

//取消按键的槽函数
void Widget::on_cancleBtn_clicked()
{
    alarmSet = false;
}

刷题:

相关推荐
郑州光合科技余经理4 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1234 天前
matlab画图工具
开发语言·matlab
dustcell.4 天前
haproxy七层代理
java·开发语言·前端
norlan_jame4 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone4 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054964 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月4 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
m0_531237174 天前
C语言-数组练习进阶
c语言·开发语言·算法
Railshiqian4 天前
给android源码下的模拟器添加两个后排屏的修改
android·开发语言·javascript
雪人不是菜鸡4 天前
简单工厂模式
开发语言·算法·c#