#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setFixedSize(800,520);
ui->textEdit->resize(800,450);
ui->textEdit->move(0,0);
ui->fontBtn->resize(100,30);
ui->fontBtn->move(205,460);
ui->colorBtn->resize(100,30);
ui->colorBtn->move(310,460);
ui->openBtn->resize(100,30);
ui->openBtn->move(415,460);
ui->saveBtn->resize(100,30);
ui->saveBtn->move(520,460);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_fontBtn_clicked()
{
bool ok;
QFont f=QFontDialog::getFont(
&ok,
QFont("Times",14),
this);
if(ok)
{
ui->textEdit->setCurrentFont(f);
}
else
{
QMessageBox::information(this,"wrong","that you selected is wrong");
}
}
void MainWindow::on_colorBtn_clicked()
{
QColor c=QColorDialog::getColor(
QColor(125,32,58),
this,
"select color:");
if(c.isValid())
{
ui->textEdit->setTextColor(c);
}
};
void MainWindow::on_openBtn_clicked()
{
QString filename=QFileDialog::getOpenFileName(
this,
"select the file",
"./",
"all (*.*);; 文本 (*.txt);; 头(*.h)");
QFile file(filename);
if(!file.exists())
{
QMessageBox::information(
this,
"wrong",
"the file is not exists",
QMessageBox::Ok);
}
if(!file.open(QIODevice::ReadWrite))
{
QMessageBox::information(this,"wrong","file open fail");
return ;
}
QByteArray msg = file.readAll();
file.close();
ui->textEdit->setText(QString::fromLocal8Bit(msg));
}
void MainWindow::on_saveBtn_clicked()
{
QString filename=QFileDialog::getOpenFileName(
this,
"save file",
"./",
"ALL (*.*) ;; 文本 (*.txt);; 头(*.h);;");
QFile file(filename);
if(!file.exists()){
QMessageBox::information(
this,
"wrong",
"open file fail",
QMessageBox::Ok);}
QString msg=ui->textEdit->toPlainText()+"\n";
file.write(msg.toLocal8Bit());
file.close();
QMessageBox::information(this,"success","save file success",QMessageBox::Ok);
};