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;
}

刷题:

相关推荐
用户805533698032 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner2 天前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz7 天前
QML Hello World 入门示例
qt
xcyxiner10 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner10 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner11 天前
DicomViewer (添加模型类)3
qt
xcyxiner11 天前
DicomViewer (目录调整) 2
qt
xcyxiner11 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
LDR00613 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术13 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript