优化文本编辑器界面
main.c
#include "mainwindow.h"
#include "ui_mainwindow.h"
void UI_Backgrand(MainWindow *Ui);
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
fileName=new QString;
timer1 = new QTimer(this);
timer1->start(33);
ui->setupUi(this);
UI_Backgrand(this);
Main_edit= new QTextEdit(this);
Main_edit->setStyleSheet("border:none;background:transparent;font-size:25px;");
btn1=new QPushButton(this);
btn1->setText("字体");
btn1->resize(70,40);
btn1->setStyleSheet("background:rgba(255,255,255,0.5);border-radius:6px;color:#000;font-size:20px;");
btn2=new QPushButton(this);
btn2->setText("颜色");
btn2->resize(70,40);
btn2->setStyleSheet("background:rgba(255,255,255,0.5);border-radius:6px;color:#000;font-size:20px;");
connect(btn1,&QPushButton::clicked,this,[=]()
{
MainWindow::Font_Select();
});
connect(btn2,&QPushButton::clicked,this,[=]()
{
MainWindow::Color_Select();
});
btn3=new QPushButton(this);
btn3->setText("打开");
btn3->resize(70,40);
btn3->setStyleSheet("background:rgba(255,255,255,0.5);border-radius:6px;color:#000;font-size:20px;");
connect(btn3,&QPushButton::clicked,this,[=]()
{
MainWindow::Open_Flie();
});
btn4=new QPushButton(this);
btn4->setText("保存");
btn4->resize(70,40);
btn4->setStyleSheet("background:rgba(255,255,255,0.5);border-radius:6px;color:#000;font-size:20px;");
connect(btn4,&QPushButton::clicked,this,[=]()
{
MainWindow::saveFlie();
});
connect(timer1,&QTimer::timeout,this,[this](){
if(this->size()!=Main_edit->size())
{
QSize this_size;
Main_edit->resize(this->size().width(),this->size().height()-70);
this_size=this->size();
int btnPos=this_size.width()-280;
btnPos=btnPos/5;
btn1->move(btnPos,this_size.height()-50);
btn2->move(btnPos*2+70,this_size.height()-50);
btn3->move(btnPos*3+140,this_size.height()-50);
btn4->move(btnPos*4+210,this_size.height()-50);
}
});
}
MainWindow::~MainWindow()
{
delete ui;
}
void UI_Backgrand(MainWindow *Ui)
{
Ui->setMaximumSize(1000,714);
Ui->setMinimumSize(647,400);
Ui->setWindowFlag(Qt::FramelessWindowHint,0);
Ui->setWindowTitle("Text");
Ui->setWindowIcon(QIcon(":/icon/QQ1.png"));
QLabel *backgrand = new QLabel(Ui);
QPixmap picture1;
picture1.load(":/icon/p0.png");
backgrand -> setPixmap(picture1);
backgrand->setScaledContents(1);
backgrand -> resize(1000,714);
Ui->setStyleSheet("color:black;font-size:20px;");
}
void MainWindow::Font_Select()
{
bool ok;
QFont Font_Text = QFontDialog::getFont(
&ok,
QFont("宋体",10,2,false),
this,
"选择字体");
if(ok)
{
this->Main_edit->setCurrentFont(Font_Text);
}
}
void MainWindow::Color_Select()
{
QColor color= QColorDialog::getColor(
QColor(0,255,255),
this,
"选中颜色");
if(color.isValid())
{
this->Main_edit->setTextBackgroundColor(color);
}
}
void MainWindow::Open_Flie()
{
*fileName=QFileDialog::getOpenFileName(
this,
"选择文件",
"./",
"所有文件(*.*);;头文件(*.h);;图片(*.png *.jpg *.xpm *.bmp *.GIF *.psd);;文本(*.txt)");
qDebug() << *fileName;
QFile file(*fileName);
if(!file.exists()&&*fileName!=NULL)
{
QMessageBox::information(this,"提示","文件不存在!");
return ;
}
if(!file.open(QIODevice::ReadWrite)&&*fileName!=NULL)
{
QMessageBox::information(this,"错误","文件打开失败!");
return ;
}
QByteArray MSG=file.readAll();
file.close();
this->Main_edit->setText(MSG);
}
void MainWindow::saveFlie()
{
QFile *file = new QFile(*fileName);
if(!file->open(QIODevice::ReadWrite|QIODevice::Text))
{
return ;
}
QString c = this->Main_edit->toPlainText();
file->write(c.toUtf8());
file->close();
}
.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTextEdit>
#include <QDebug>
#include <QIcon>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>
#include <QFontDialog>
#include <QColorDialog>
#include <QFileDialog>
#include <QTimer>
#include <QScreen>
#include <QWidget>
#include <QWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void Font_Select();
void Color_Select();
void Open_Flie();
void saveFlie();
private:
Ui::MainWindow *ui;
QPushButton *btn1;
QPushButton *btn2;
QPushButton *btn3;
QPushButton *btn4;
QLabel *lab1;
QLabel *lab2;
QLineEdit *edit1;
QLineEdit *edit2;
QTextEdit *Main_edit;
QTimer *timer1;
QScreen *Refresh;
QString *fileName;
};
#endif // MAINWINDOW_H