完成文本编辑器的保存功能
firstwindow.cpp
#include "firstwindow.h"
#include "ui_firstwindow.h"
FirstWindow::FirstWindow(QWidget *parent)
: QWidget(parent)
, ui(new Ui::FirstWindow)
{
ui->setupUi(this);
}
FirstWindow::~FirstWindow()
{
delete ui;
}
//字体
void FirstWindow::on_btn1_clicked()
{
bool ok;
QFont f = QFontDialog::getFont(&ok,
QFont("楷体",20,10,false),
this,
"选择字体");
if(ok)
{
ui->textEdit->setFont(f);
}else
{
QMessageBox::information(this,"提示","您没有选择字体");
}
}
//颜色
void FirstWindow::on_btn2_clicked()
{
QColor c = QColorDialog::getColor(QColor(90,100,170),
this,
"选择颜色");
if(c.isValid())
{
ui->textEdit->setTextColor(c);
//ui->textEdit->setTextBackgroundColor(c);
}else {
QMessageBox::information(this,"提示","您没有选择颜色");
}
}
//打开
void FirstWindow::on_btn3_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this,
"选择文件",
"./",
"all file(*.*)");
QFile file(fileName);
if(!file.open(QFile::ReadOnly))
{
return;
}
QByteArray msg = file.readAll();
ui->textEdit->setText(msg);
file.close();
}
//保存
void FirstWindow::on_btn4_clicked()
{
QString fileName = QFileDialog::getSaveFileName(this,
"保存文件",
"./",
"all file(*.*)");
QFile file(fileName);
if(!file.open(QFile::ReadWrite))
{
return;
}
file.write(ui->textEdit->toPlainText().toUtf8());
file.close();
QMessageBox::information(this,"提示","保存成功");
}