Qt 第三讲 手动拖动ui界面组件,事件处理机制,实现简单闹钟

手动拖动ui界面组件,实现闹钟

源文件

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

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

    ui->btn_end->setEnabled(false);

    speecher = new QTextToSpeech(this);

    QTime sys_time = QTime::currentTime();
    QString t = sys_time.toString("hh:mm:ss");
    ui->label_time->setText(t);
    ui->label_time->setAlignment(Qt::AlignCenter);
}

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

//开始按钮
void Widget::on_btn_start_clicked()
{
    
    ttId = startTimer(1000);//定时

    //锁定
    ui->btn_start->setEnabled(false);
    ui->jishi_edit->setEnabled(false);
    ui->label_time->setEnabled(false);
    ui->textEdit->setEnabled(false);
    //解锁
    ui->btn_end->setEnabled(true);

}

//定时器事件处理函数的定义
void Widget::timerEvent(QTimerEvent *e)
{

    if(e->timerId() == ttId){

        QTime sys_time = QTime::currentTime();
        QString t = sys_time.toString("hh:mm:ss");
        ui->label_time->setText(t);
        ui->label_time->setAlignment(Qt::AlignCenter);

        QString sj = ui->jishi_edit->text();

        if(sj == t){
            speecher->say(ui->textEdit->toPlainText());
        }
    }
}

//关闭按钮
void Widget::on_btn_end_clicked()
{
    this->killTimer(tId);
    //解锁
    ui->btn_start->setEnabled(true);
    ui->jishi_edit->setEnabled(true);
    ui->label_time->setEnabled(true);
    ui->textEdit->setEnabled(true);
    //锁定
    ui->btn_end->setEnabled(false);


}

头文件

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTimerEvent> //定时器时间处理函数
#include <QTime> //时间类
#include <QLabel>
#include <QtTextToSpeech>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
     int tId;
     int ttId;

     void timerEvent(QTimerEvent *e);

     QTextToSpeech *speecher;


//重写的关于定时器事件的处理函数
private slots:
    void on_btn_start_clicked();
    
    void on_btn_end_clicked();

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H
相关推荐
karmueo4629 分钟前
视频序列和射频信号多模态融合算法Fusion-Vital解读
算法·音视频·多模态
小汉堡编程1 小时前
数据结构——vector数组c++(超详细)
数据结构·c++
写代码的小球3 小时前
求模运算符c
算法
weixin_472339464 小时前
高效处理大体积Excel文件的Java技术方案解析
java·开发语言·excel
枯萎穿心攻击4 小时前
响应式编程入门教程第二节:构建 ObservableProperty<T> — 封装 ReactiveProperty 的高级用法
开发语言·unity·c#·游戏引擎
Eiceblue6 小时前
【免费.NET方案】CSV到PDF与DataTable的快速转换
开发语言·pdf·c#·.net
tan180°6 小时前
MySQL表的操作(3)
linux·数据库·c++·vscode·后端·mysql
m0_555762906 小时前
Matlab 频谱分析 (Spectral Analysis)
开发语言·matlab
大千AI助手7 小时前
DTW模版匹配:弹性对齐的时间序列相似度度量算法
人工智能·算法·机器学习·数据挖掘·模版匹配·dtw模版匹配