C++&QT---QT-day3

cpp 复制代码
#include "widget.h"
#include "ui_widget.h"
//需要在.pro文件第一行加 texttospeech

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->lineEdit->setPlaceholderText("时:分:秒");//设定框占位字符
    //ui->textEdit->setText("春眠不觉晓");
    ui->lineEdit->setAlignment(Qt::AlignCenter);//居中显示
    ui->textEdit->setAlignment(Qt::AlignCenter);
    speecher=new QTextToSpeech(this);//给播报员分配空间
}

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

void Widget:: timerEvent(QTimerEvent *e)
{
    if(e->timerId() == tId)
    {
        QTime sys_time = QTime::currentTime(); //获取当前系统时间
        QString s = sys_time.toString("hh:mm:ss");//把系统时间转换成字符串
        ui->lab_time->setText(s);//将系统时间放入标签中
        ui->lab_time->setAlignment(Qt::AlignCenter);//居中显示
        if(s == ui->lineEdit->text())//判断设定时间与系统时间是否匹配
        {
            speecher->say(ui->textEdit->toPlainText());
        }
    }
}

void Widget::on_startBtn_clicked()//定时器启动事件
{
    tId=startTimer(1000);
}

void Widget::on_stopBtn_clicked()//定时器关闭事件
{
    this->killTimer(tId);
}
cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTimerEvent>
#include <QTime>
#include <QTimer>
#include <QLabel>
#include <QLineEdit>
#include <QTextToSpeech>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    void timerEvent(QTimerEvent *e);

private slots:
    void on_startBtn_clicked();

    void on_stopBtn_clicked();

private:
    Ui::Widget *ui;
    int tId;
    QTextToSpeech *speecher;
};
#endif // WIDGET_H
相关推荐
七夜zippoe16 分钟前
为什么 2026 年每个 Java 团队都该懂 AI Agent
java·开发语言·人工智能
znnnk22 分钟前
【Python】GUI 开发从入门到实战(三):PyQt/PySide 进阶之路
开发语言·python·pyqt
Despacito100640 分钟前
Java后端性能探查工具速查表
java·开发语言
晊晌_h1 小时前
嵌入式从0到精通——Linux C|TCP 并发服务器实现方案详解
服务器·开发语言·tcp/ip
传奇开心果编程1 小时前
【Rust入门知识点学与练】第17课:闭包 Closures
开发语言·学习·rust
广州灵眸科技有限公司2 小时前
从手动三步到上电即连:4G模组systemd开机自连方案
linux·运维·服务器·开发语言·网络·人工智能·php
今夜有雨.2 小时前
C# 学习文档(零基础入门)
开发语言·学习·c#
软件黑马王子2 小时前
14.事件中心:主要作用和原理
开发语言·前端框架·c#
君顾13 小时前
酒吧点餐小程序系统开发实战:从架构设计到上线全流程指南
java·开发语言·酒吧