1.一个闹钟
widget.cpp:
#include "widget.h"
#include "ui_widget.h"#include <QWidget>
#include <QTimerEvent> //定时器事件处理类
#include <QTime>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{//给播报员实例化空间speecher = new QTextToSpeech(this);ui->setupUi(this);time_id=this->startTimer(1);flag=0;}Widget::~Widget()
{delete ui;this->killTimer(time_id);
}void Widget::timerEvent(QTimerEvent *e)
{if(e->timerId()==time_id) //说明定时器1到位{QTime sys_t = QTime::currentTime(); //获取系统时间//将QTime类对象转化为字符串QString t=sys_t.toString("hh:mm:ss");//展示到ui界面ui->nowtimeLab->setText(t);//语音播报脑子if(1==flag){QString gotime;gotime=ui->hourEdit->toPlainText()+":"+ui->fengEdit->toPlainText()+":"+ui->miaoEdit->toPlainText();if(gotime==t){ssssay();}}}
}void Widget::ssssay()
{QString str;str=ui->textEdit->toPlainText();speecher->say(str);
}void Widget::on_startBtn_clicked()
{flag=1;ui->textEdit->setEnabled(false);
}void Widget::on_cancelBtn_clicked()
{speecher->stop();flag=0;ui->textEdit->setEnabled(true);
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QTimerEvent> //定时器事件处理类
#include <QTime>
#include <QTextToSpeech> //文本转语音类
#include<QVoice>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;void ssssay();private slots:void on_startBtn_clicked();void on_cancelBtn_clicked();private:Ui::Widget *ui;int time_id;//定义一个播报员QTextToSpeech *speecher;int flag;
};
#endif // WIDGET_H
2.文件保存
QString Filename=QFileDialog::getSaveFileName(this,//父组件"保存文件","./",//起始路径"All(*.*)");//判断文件是否存在if(Filename.isNull()){QMessageBox::information(this,"提示","用户取消保存文件");return;}else{//qDebug()<<Filename;//文件操作//1、实例化一个文件对象QFile file(Filename);//2、打开文件//3、读写操作//1、实例化一个文件对象file.open(QIODevice::WriteOnly);//创建文件,且权限为只写QString msg = ui->textEdit->toPlainText();//获取编辑器的文本内容QByteArray saveName;saveName.append(msg);file.write(saveName);file.close();//4、关闭文件file.close();}