<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"><class>Widget</class><widget class="QWidget" name="Widget"><property name="geometry"><rect><x>0</x><y>0</y><width>800</width><height>600</height></rect></property><property name="font"><font><pointsize>10</pointsize></font></property><property name="windowTitle"><string>Widget</string></property><widget class="QLabel" name="ltime"><property name="geometry"><rect><x>90</x><y>130</y><width>211</width><height>81</height></rect></property><property name="sizePolicy"><sizepolicy hsizetype="Preferred" vsizetype="Preferred"><horstretch>0</horstretch><verstretch>0</verstretch></sizepolicy></property><property name="font"><font><pointsize>17</pointsize><weight>75</weight><bold>true</bold></font></property><property name="styleSheet"><string notr="true">background-color: rgb(255, 255, 255);</string></property><property name="text"><string/></property><property name="alignment"><set>Qt::AlignCenter</set></property></widget><widget class="QLineEdit" name="timer"><property name="geometry"><rect><x>370</x><y>130</y><width>171</width><height>41</height></rect></property><property name="font"><font><pointsize>13</pointsize></font></property><property name="text"><string/></property><property name="alignment"><set>Qt::AlignCenter</set></property></widget><widget class="QPushButton" name="start"><property name="geometry"><rect><x>370</x><y>180</y><width>70</width><height>40</height></rect></property><property name="font"><font><pointsize>13</pointsize><weight>75</weight><bold>true</bold></font></property><property name="text"><string>启动</string></property></widget><widget class="QPushButton" name="stop"><property name="geometry"><rect><x>460</x><y>180</y><width>70</width><height>40</height></rect></property><property name="font"><font><pointsize>13</pointsize><weight>75</weight><bold>true</bold></font></property><property name="text"><string>停止</string></property></widget><widget class="QTextEdit" name="textbox"><property name="geometry"><rect><x>90</x><y>260</y><width>451</width><height>231</height></rect></property><property name="font"><font><pointsize>16</pointsize></font></property></widget><widget class="QPushButton" name="load"><property name="geometry"><rect><x>330</x><y>520</y><width>200</width><height>40</height></rect></property><property name="font"><font><pointsize>14</pointsize><weight>75</weight><bold>true</bold></font></property><property name="text"><string>保存</string></property></widget><widget class="QPushButton" name="pushButton"><property name="geometry"><rect><x>100</x><y>520</y><width>200</width><height>40</height></rect></property><property name="font"><font><pointsize>14</pointsize><weight>75</weight><bold>true</bold></font></property><property name="text"><string>打开</string></property></widget></widget><resources/><connections/>
</ui>
#include<QTime>
#include<QString>
#include<QFile>
#include<QMessageBox>
#include<QTextToSpeech>
#include<QDebug>
#include<QFileDialog>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();void timerEvent(QTimerEvent *e) override;private slots:void on_start_clicked();void on_stop_clicked();void on_load_clicked();void on_pushButton_clicked();private:Ui::Widget *ui;//当时时间号int time_id;//查询时间号int timer_id;//语音QTextToSpeech * speech;//文件QFile *file;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
int i = 1;
Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);speech = new QTextToSpeech(this);time_id = this->startTimer(1000);}Widget::~Widget()
{delete ui;
}void Widget::on_start_clicked()
{QString t = ui->timer->text();if(t==NULL)QMessageBox::critical(this,"错误","请输入正确时间");elsetimer_id = this->startTimer(100);
}void Widget::on_stop_clicked()
{killTimer(timer_id);
}
void Widget::timerEvent(QTimerEvent *e)
{if(e->timerId() == time_id){QTime time = QTime::currentTime();QString s = time.toString("hh:mm:ss");ui->ltime->setText(s);}if(e->timerId() == timer_id){if(ui->ltime->text() == ui->timer->text()){speech->say(ui->textbox->document()->toPlainText());if(i == 5){i = 1;killTimer(timer_id);}}}
}void Widget::on_load_clicked()
{QString fname = QFileDialog::getSaveFileName(this,"选择文件","D:/","ALL(*.*);;Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)");if(fname!=NULL){//实例化一个文件对象QFile file(fname);//存储内容QString ba;//打开文件ba = ui->textbox->document()->toPlainText();if(!file.isOpen())if(!file.open(QIODevice::WriteOnly)){QMessageBox::critical(this,"","打开文件失败");return;}file.write(ba.toUtf8());file.close();}
}void Widget::on_pushButton_clicked()
{QString fname = QFileDialog::getOpenFileName(this,"选择文件","D:/","ALL(*.*);;Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)");if(fname!=NULL){//实例化一个文件对象QFile file(fname);//存储内容QByteArray ba;//打开文件if(!file.isOpen())if(!file.open(QIODevice::ReadOnly)){QMessageBox::critical(this,"","打开文件失败");return;}ba = file.readAll();file.close();ui->textbox->setText(ba);}elseui->textbox->setText("时间到");
}
#include "widget.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();return a.exec();
}