.h
- #ifndef WIDGET_H
- #define WIDGET_H
-
- #include
- #include
- #include
- #include
- #include
- #include
- #include
-
- #include
- 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) override;
-
- private slots:
- void on_openBtn_clicked();
-
- void on_closeBtn_clicked();
-
- private:
- Ui::Widget *ui;
-
- int tim;
- int respeech_time;
- //将组建设置为私有成员
- QTextEdit *speakEdit;
- //定义一个播报员
- QTextToSpeech *speech;
- };
- #endif // WIDGET_H
-
.cpp
- #include "widget.h"
- #include "ui_widget.h"
-
- Widget::Widget(QWidget *parent)
- : QWidget(parent)
- , ui(new Ui::Widget)
- {
- ui->setupUi(this);
-
- //更改窗口标题
- this->setWindowTitle("查理苏的专属闹钟");
- ui->logo->setPixmap(QPixmap(":/qt/a1.webp"));
- ui->logo->setScaledContents(true);
-
- //用户名密码图
-
- ui->larmlab->setText("早安,我的未婚妻");
-
- speech = new QTextToSpeech(this);
-
- tim=this->startTimer(1000);
- }
-
- Widget::~Widget()
- {
- delete ui;
- }
-
- void Widget::on_openBtn_clicked()
- {
- respeech_time=this->startTimer(1000);
- //功能:启动一个定时器
-
- //参数:超时时间
- //返回值:当前id号
- // 创建一个QTextToSpeech对象
- QTextToSpeech *speech = new QTextToSpeech(this);
-
- // 设置语音输出的声音和语速
- speech->setVolume(2.0);
- speech->setRate(0.1);
-
- // 设置要说的文本
- QString text = "早安,我的未婚妻";
-
- // 语音播放文本
-
- respeech_time=this->startTimer(5000);
- speech->say(text);
-
- }
-
- void Widget::on_closeBtn_clicked()
- {
- speech->stop();
- QTime sys_t=QTime::currentTime();
- QString t=sys_t.toString("hh:mm:ss");
- ui->lineEdit->setText(t);
- }
- //定时器事件
- void Widget::timerEvent(QTimerEvent *e)
- {
- if(e->timerId()==tim)
- {
- QTime sys_t=QTime::currentTime();
- QString t=sys_t.toString("hh:mm:ss");
- //展示到ui界面
- ui->syslab->setText(t);
-
- }else if(e->timerId()==respeech_time)
- {
- QTextToSpeech *speech = new QTextToSpeech(this);
-
- // 设置语音输出的声音和语速
- speech->setVolume(2.0);
- speech->setRate(0.1);
-
- // 设置要说的文本
- QString text = "早安,我的未婚妻";
-
- // 语音播放文本
-
- respeech_time=this->startTimer(5000);
- speech->say(text);
- }
- }
- #include "widget.h"
-
- #include
-
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- Widget w;
- w.show();
- return a.exec();
- }
