//.pro配置文件
#-------------------------------------------------
#
# Project created by QtCreator 2023-06-15T11:31:23
#
#-------------------------------------------------
QT += core gui texttospeech
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = alarm_clock
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++11
SOURCES += \
main.cpp \
my_qt.cpp
HEADERS += \
my_qt.h
FORMS += \
my_qt.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
头文件
#ifndef MY_QT_H
#define MY_QT_H
#include <QWidget>
#include <QTimerEvent> //事件类
#include <QDateTime> //日期时间类
#include <QDebug>
#include <QTextToSpeech>
#include <QPushButton>
namespace Ui {
class my_qt;
}
class my_qt : public QWidget
{
Q_OBJECT
public:
explicit my_qt(QWidget *parent = nullptr);
~my_qt();
void timerEvent(QTimerEvent *e)override;
private slots:
void on_btn_start_clicked();
void on_btn_close_clicked();
private:
Ui::my_qt *ui;
int tid,tid2;
QTextToSpeech *tell;
};
#endif // MY_QT_H
main函数
#include "my_qt.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
my_qt w;
w.show();
return a.exec();
}
运行函数
#include "my_qt.h"
#include "ui_my_qt.h"
my_qt::my_qt(QWidget *parent) :
QWidget(parent),
ui(new Ui::my_qt)
{
ui->setupUi(this);
//启动一个定时器
tid = this->startTimer(1000);
tell = new QTextToSpeech(this);
tell->setLocale(QLocale::Chinese);
}
my_qt::~my_qt()
{
delete ui;
}
void my_qt::on_btn_start_clicked()
{
//将启动按钮禁用
ui->btn_start->setEnabled(0);
//将文本框禁用
ui->textEdit->setEnabled(0);
ui->timer_label->setEnabled(0);
}
void my_qt::on_btn_close_clicked()
{
//将启动按钮启用
ui->btn_start->setEnabled(1);
//将文本框启用
ui->textEdit->setEnabled(1);
ui->timer_label->setEnabled(1);
//关闭一个定时器
this->killTimer(tid);
}
void my_qt::timerEvent(QTimerEvent *e){
if(e->timerId() == tid){
ui->time_label->setAlignment(Qt::AlignCenter);//将显示文本居中显示
QDateTime t = QDateTime::currentDateTime();
ui->time_label->setText(t.toString("yyyy/MM/dd hh:mm:ss"));
if(ui->timer_label->text()== ui->time_label->text()){
tell->say(ui->textEdit->toPlainText());
}
}
}