#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_zibtn_clicked()
{
bool ok; //判断是否选择字体
QFont f = QFontDialog::getFont(&ok, //判断是否选择字体
QFont("隶书",10,2, true),//初始字体
nullptr, //父组件
"选择字体"); //对话框标题
//对ok进行判断判断是否选中了字体
if(ok)
{
//将选中的字体加载到文本编辑器的文本内容
ui->textEdit->setCurrentFont(f);
}
}
void Widget::on_yanbtn_clicked()
{
QColor c = QColorDialog::getColor(QColor("blue"),
this,
"选择颜色");
//对颜色判断是否合法
if(c.isValid())
{
//将颜色设置到字体上
ui->textEdit->setTextColor(c);//设置选中字体的前景色
// ui->textEdit->setTextBackgroundColor(c);//设置选中字体的背景色
}
}
void Widget::on_openbtn_clicked()
{
QString filename = QFileDialog::getOpenFileName(this,
"选择文件",
"./",
"ALL(*.*);;Text(*.txt);;源文件(*.cpp);;图片(*.png)");
//输入文件路径
qDebug() << filename;
QFile file(filename);
if(!file.exists())
{
qDebug() << "文件不存在";
return ;
}
if(!file.open(QIODevice::ReadWrite))
{
qDebug() << "文件打开失败";
return ;
}
//读取文件中的数据
QByteArray text = file.readAll();//将文件中所有数据都读取出来
//将读取的数据展示到界面上
ui->textEdit->setText(QString::fromLocal8Bit(text));
//关闭文件
file.close();
}
void Widget::on_closebtn_clicked()
{
//获取文件路径
QString filename = QFileDialog::getSaveFileName(this,
"保存文件",
"./",
"ALL(*.*)");
//实例化文件对象
QFile file(filename);
//打开文件
if(!file.open(QIODevice::ReadWrite))
{
qDebug() << "文件打开失败";
return;
}
//获取UI界面的文本内容
QString msg = ui->textEdit->toPlainText();
//将msg写入文件
file.write(msg.toLocal8Bit());
//关闭文件
file.close();
}