1.头文件
#ifndef WIDGET_H
#define WIDGET_H
#include
#include
#include
#include
#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();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
//重写定时器处理函数
void timerEvent(QTimerEvent *e);
private:
Ui::Widget *ui;
int timer_id; //定时器的id号
QTextToSpeech *s;
};
#endif // WIDGET_H
2.源文件
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
timer_id=0;
s = new QTextToSpeech(this);
connect(this,SIGNAL(clicked),this,SLOT(my_slot));
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
timer_id = this->startTimer(1000);
}
void Widget::on_pushButton_2_clicked()
{
this->killTimer(timer_id);
}
void Widget::timerEvent(QTimerEvent *e)
{
if(e->timerId() == timer_id)
{
QString userInputTime = ui->lineEdit->text();
QTime sys_t = QTime::currentTime(); //获取系统时间
//将QTime类对象转换为字符串
QString t = sys_t.toString("hh:mm:ss");
ui->label->setText(t);
if(t == userInputTime)
{
//QMessageBox::information(this,"成功","时间匹配成功");
qDebug()<<"时间匹配成功";
s->say(ui->textEdit->toPlainText());
this->killTimer(timer_id);
timer_id=0;
}
}
}
二 、保存
1.头文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
void on_pushButton_4_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
2.源文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
//字体按钮槽函数
void MainWindow::on_pushButton_clicked()
{
bool ok; //返回用户是否选中字体
//调用getFont获取字体对话框函数
QFont f = QFontDialog::getFont(&ok,
QFont("隶属",10,10,false)
,this,
"选择字体");
if(ok)
{
//ui->textEdit->setFont(f);
ui->textEdit->setCurrentFont(f);
}else
{
QMessageBox::information(this,"取消","用户取消");
}
}
//颜色按钮
void MainWindow::on_pushButton_2_clicked()
{
QColor c = QColorDialog::getColor(QColor("pink"),
this,
"选择颜色");
if(c.isValid())
{
ui->textEdit->setTextBackgroundColor(c);
}else
{
QMessageBox::information(this,"取消","用户取消");
}
}
void MainWindow::on_pushButton_3_clicked()
{
QString filename = QFileDialog::getOpenFileName(this,
"选择文件",
"./",
"ALL(*.*);;Images(*.png *.xpm *.jpg);;Text files(*.txt);;XML files(*.xml)");
if(filename.isNull())
{
QMessageBox::information(this,"提示","用户取消");
return;
}
qDebug()<textEdit->setText(msg);
}
void MainWindow::on_pushButton_4_clicked()
{
QString f = QFileDialog::getSaveFileName(this,
"保存文件",
"./",
"ALL(*.*);;"
"Text files(*.txt);;"
"Images(*.png,*.jpg)");
if(f.isNull())
{
QMessageBox::information(this,"提示","用户取消保存文件");
return;
}
QFile file(f);
if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QMessageBox::critical(this,"错误","无法打开文件进行保存");
}
QString textToSave = ui->textEdit->toPlainText();
QTextStream out(&file);
out<