完成文本编辑器的保存工作
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#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_fontbtn_clicked();
void on_colorbtn_clicked();
void on_openbtn_clicked();
void on_savebtn_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_fontbtn_clicked()
{
bool ok;
QFont f = QFontDialog::getFont(&ok,
QFont("宋体",10,10,false),
this,
"选择字体");
if(ok)
{
ui->textEdit->setCurrentFont(f);
}
else
{
QMessageBox::information(this,"取消","用户取消字体");
}
}
void Widget::on_colorbtn_clicked()
{
QColor c = QColorDialog::getColor(QColor("yellow"),
this,
"选择颜色");
if(c.isValid())
{
ui->textEdit->setTextBackgroundColor(c);
}
else
{
QMessageBox::information(this,"取消","用户取消颜色");
}
}
void Widget::on_openbtn_clicked()
{
QString filename = QFileDialog::getOpenFileName(this,
"选择文件",
"./",
"all(*.*)");
if(filename.isNull())
{
QMessageBox::information(this,"取消","用户取消选择文件");
}
else
{
qDebug()<textEdit->setText(msg);
}
}
void Widget::on_savebtn_clicked()
{
QString fileName = QFileDialog::getSaveFileName(this, "保存文件", "./", "All(*.*)");
if(fileName.isNull())
{
QMessageBox::information(this, "取消", "用户取消选中");
}
else
{
QFile file(fileName);
if(file.exists())
{
file.remove();
}
if(!file.open(QIODevice::WriteOnly))
{
QMessageBox::critical(this, "错误", "文件打开失败");
return;
}
else
{
std::string s1 = ui->textEdit->toPlainText().toStdString();
const char *text = s1.c_str();
if(-1 == file.write(text))
{
QMessageBox::critical(this, "错误", "文件写入失败");
}else
{
QMessageBox::information(this, "成功", "文件写入成功");
}
file.close();
}
}
}
main.cpp
#include "widget.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
#ifndef WIDGET_H
#define WIDGET_H
#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 *e) override; //定时器事件处理函数
signals:
void my_sign();
private slots:
void qidong_clicked();
void tingzhi_clicked();
private:
Ui::Widget *ui;
int timer_id; //定时器的id号
QLabel *lab1;
QLineEdit *edit1;
QTextEdit *edit2;
QTextToSpeech *speencher;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include
#include
#include
#include
#include
#include
#include
#include //定时器事件处理类
#include
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
QPushButton *btn1 = new QPushButton; //设置按钮1
btn1->setParent(this);
btn1->setText("启动"); //定义按钮内容
btn1->resize(100,50); //设置按钮大小
btn1->move(400,200); //移动按钮位置
QPushButton *btn2 = new QPushButton; //设置按钮2
btn2->setParent(this);
btn2->setText("停止"); //定义按钮内容
btn2->resize(100,50); //设置按钮大小
btn2->move(600,200);
lab1 = new QLabel(this); //设置标签
lab1->resize(100,50);
lab1->setStyleSheet("background-color:yellow;");
//lab1->setText("");
edit1 = new QLineEdit(this);
edit1->resize(300,75); //设置行编辑器大小
edit1->move(400,80); //移动行编辑器
edit1->setText(QString());
edit2 = new QTextEdit(this);
edit2->resize(400,300);
edit2->move(200,300);
edit2->setText(QString("南华城里月如昼\n十二玉楼非吾乡"));
speencher = new QTextToSpeech(this);
connect(btn1, &QPushButton::clicked, this, &Widget::qidong_clicked);
connect(btn2, &QPushButton::clicked, this, &Widget::tingzhi_clicked);
}
Widget::~Widget()
{
delete ui;
}
//启动定时器按钮对应的槽函数
void Widget::qidong_clicked()
{
timer_id = this->startTimer(1000);
//功能:启动一个定时器
}
//关闭定时器按钮对应的槽函数
void Widget::tingzhi_clicked()
{
this->killTimer(timer_id); //关闭给定的定时器
}
//定时器事件处理函数
void Widget::timerEvent(QTimerEvent *e)
{
if(e->timerId() == timer_id) //说明定时器1到位
{
QTime sys_t = QTime::currentTime(); //获取系统时间
//将QTime类对象转换为字符串
QString t = sys_t.toString("hh:mm:ss");
//展示到ui界面
lab1->setText(t);
if(t == edit1->text())
{
speencher->say(edit2->toPlainText());
qDebug()<toPlainText();
}
}
}
main.cpp
#include "widget.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}