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
相关推荐
Victoria.a1 小时前
string类详解
数据结构·c++
转调3 小时前
第九章:内存池的调整与测试
c++·内存池
行路见知3 小时前
1.5 Go切片使用
开发语言·golang
XY_墨莲伊4 小时前
【算法设计与分析】实验5:贪心算法—装载及背包问题
c语言·数据结构·c++·算法·贪心算法·排序算法
KuaCpp5 小时前
搜索与图论复习2最短路
c++·算法·图论
子燕若水5 小时前
uv 安装包
开发语言·chrome·python
Lenyiin5 小时前
《 C++ 点滴漫谈: 二十五 》空指针,隐秘而危险的杀手:程序崩溃的真凶就在你眼前!
c++·nullptr·lenyiin·c++关键字
zxb@hny6 小时前
vscode命令面板输入 CMake:build不执行提示输入
c++·ide·vscode
c-c-developer6 小时前
C++ Primer 自定义数据结构
数据结构·c++
不会打代码呜呜呜呜6 小时前
小白零基础--CPP多线程
开发语言·c++·算法