- QT += core gui texttospeech
-
- greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
-
- CONFIG += c++11
-
- # The following define makes your compiler emit warnings if you use
- # any Qt feature that has been marked 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 it uses 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
-
- SOURCES += \
- main.cpp \
- widget.cpp
-
- HEADERS += \
- widget.h
-
- FORMS += \
- widget.ui
-
- # Default rules for deployment.
- qnx: target.path = /tmp/$${TARGET}/bin
- else: unix:!android: target.path = /opt/$${TARGET}/bin
- !isEmpty(target.path): INSTALLS += target
头文件
widght.h
- #ifndef WIDGET_H
- #define WIDGET_H
-
- #include
- #include
- #include
- #include
- #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 *event) override;
-
- private:
- Ui::Widget *ui;
-
- //定义一个lable指针, 当前时间
- QLabel *current_time_lab;
-
- //定义一个行编辑器指针, 闹钟
- QLineEdit *clock_edit;
-
- //定义一个按钮指针, 启动
- QPushButton *start_clock_btn;
-
- //定义一个停止闹钟, 关闭
- QPushButton *stop_clock_btn;
-
- //定义一个文本编辑器
- QTextEdit *text_edit;
-
- //定义一个闹钟语音播放者
- QTextToSpeech *clock_speecher;
-
- //定义一个定时器指针
- QTimer *clock_timer;
- QTimer *clock_timer2;
-
- int time_id;
-
- public slots:
- void start_slot();
- void cklock_slot();
- void stop_slot();
-
-
- };
- #endif // WIDGET_H
源文件
main.cpp
- #include "widget.h"
-
- #include
-
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- Widget w;
- w.show();
- return a.exec();
- }
widget.cpp
- #include "widget.h"
- #include "ui_widget.h"
-
- int flag = 0;
- int clock_flag = 0;
- Widget::Widget(QWidget *parent)
- : QWidget(parent)
- , ui(new Ui::Widget)
- {
- ui->setupUi(this);
-
- //组件实例化
- current_time_lab = new QLabel(this);
- clock_edit = new QLineEdit(this);
- start_clock_btn = new QPushButton("启动", this);
- stop_clock_btn = new QPushButton("停止", this);
- text_edit = new QTextEdit(this);
- clock_speecher = new QTextToSpeech();
- clock_timer = new QTimer(this);
- clock_timer2 = new QTimer(this);
-
- //设置页面布局
- //固定窗口大小
- this->setFixedSize(500,400);
- //this->setStyleSheet("background-img");
- current_time_lab->resize(300, 70);
- current_time_lab->move(40,30);
- current_time_lab->setStyleSheet("background-color:rgba(137, 199, 117, 0.7);font:幼圆; font-size:26px;");
- current_time_lab->setAlignment(Qt::AlignCenter);
-
-
- clock_edit->resize(100, 30);
- //clock_edit->setPlaceholderText("定时时间");
- clock_edit->move(this->width()-140, 30);
- clock_edit->setAlignment(Qt::AlignHCenter);
-
-
- start_clock_btn->resize(45, 30);
- start_clock_btn->move(clock_edit->x(), clock_edit->y()+40);
- start_clock_btn->setStyleSheet("font:幼圆; font-size:10px; padding:2px");
-
- stop_clock_btn->resize(start_clock_btn->size());
- stop_clock_btn->move(start_clock_btn->x()+55, start_clock_btn->y());
- stop_clock_btn->setStyleSheet("font:幼圆; font-size:10px; padding:2px");
-
- text_edit->resize(this->width()-80, 220);
- text_edit->move(current_time_lab->x(), current_time_lab->y()+current_time_lab->height()+40);
-
- stop_clock_btn->setDisabled(true);
-
- clock_timer->start(1000);
- connect(clock_timer, &QTimer::timeout, this, [&](){
- QDateTime date_time = QDateTime::currentDateTime();
- current_time_lab->setText(date_time.toString("yyyy-MM-dd hh:mm:ss"));
- QTime currenttime = QTime::currentTime();
- QString time = currenttime.toString("hh:mm:ss");
- if(time <= clock_edit->text())
- {
- clock_flag = 0;
- }
- });
- connect(clock_timer, &QTimer::timeout, this, &Widget::cklock_slot);
-
- connect(start_clock_btn, &QPushButton::clicked, this, &Widget::start_slot);
- connect(stop_clock_btn, &QPushButton::clicked, this, &Widget::stop_slot);
-
- //time_id = startTimer(0);
-
- }
-
- Widget::~Widget()
- {
- delete ui;
- }
-
- void Widget::start_slot()
- {
- start_clock_btn->setEnabled(false);
- stop_clock_btn->setEnabled(true);
- clock_edit->setDisabled(true);
-
- }
-
- void Widget::cklock_slot()
- {
- if(!start_clock_btn->isEnabled())
- {
-
- QTime currenttime = QTime::currentTime();
- QString time = currenttime.toString("hh:mm:ss");
-
- if(time >= clock_edit->text()&& clock_flag == 0)
- {
- if(flag == 0)
- {
- clock_speecher->say(text_edit->toPlainText());
- clock_timer2->start(2000);
- connect(clock_timer2, &QTimer::timeout, [](){
- flag = 0;});
-
- }
- flag =1;
- }
- }
- else
- clock_speecher->stop();
-
- }
-
- void Widget::stop_slot()
- {
- //clock_speecher->stop();
- start_clock_btn->setEnabled(true);
- stop_clock_btn->setEnabled(false);
- clock_edit->setEnabled(true);
- clock_flag = 1;
- }
-
- //定时器事件处理函数的实现
- void Widget::timerEvent(QTimerEvent *event)
- {
- if(event->timerId() == time_id)
- {
- //static int num = 0;
- //ui->eventtime->setNum(++num); //setNum()函数是设置文本内容数字
-
- QDateTime sys_dt = QDateTime::currentDateTime(); //获取当前系统的日期时间
- this->current_time_lab->setText(sys_dt.toString("yyyy-MM-dd hh:mm:ss"));
- this->current_time_lab->setAlignment(Qt::AlignCenter);
-
- if(!start_clock_btn->isEnabled())
- {
-
- QTime currenttime = QTime::currentTime();
- QString time = currenttime.toString("hh:mm:ss");
-
- if(time == clock_edit->text())
- {
- // if(flag == 0)
- // {
- clock_speecher->say(text_edit->toPlainText());
- // clock_timer2->start(2000);
- // connect(clock_timer2, &QTimer::timeout, [](){
- // flag = 0;});
-
- //}
- //flag =1;
- }
- }
- else
- clock_speecher->stop();
- }
- }
-
Qt简易闹钟
