用QT实现的简单文本编辑器
头文件widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
#include <QTextEdit>
#include <QFont>
#include <QFontDialog>
#include <QColor>
#include <QColorDialog>
#include <QFile>
#include <QFileDialog>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
QPushButton *font_btn; //字体按钮
QPushButton *color_btn; //颜色按钮
QPushButton *open_btn; //打开文件
QPushButton *save_btn; //保存文件
QPushButton *clear_btn; //清空按钮
QTextEdit *textedit; //文本编辑器
private slots:
void font_btn_slot();
void color_btn_slot();
void open_btn_slot();
void save_btn_slot();
void clear_btn_slot();
};
#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);
this->setMinimumSize(640, 480);
this->setWindowTitle("记事本-zzy");
//文本编辑器
textedit = new QTextEdit(this);
textedit->resize(636, 400);
textedit->move(2,0);
//设置字体
font_btn = new QPushButton("字体", this);
font_btn->resize(90,40);
font_btn->move(30, 420);
connect(font_btn, &QPushButton::clicked, this, &Widget::font_btn_slot);
//设置颜色
color_btn = new QPushButton("颜色", this);
color_btn->resize(90,40);
color_btn->move(font_btn->x()+120, font_btn->y());
connect(color_btn, &QPushButton::clicked, this, &Widget::color_btn_slot);
//打开文件
open_btn = new QPushButton("打开文件", this);
open_btn->resize(90,40);
open_btn->move(color_btn->x()+120, font_btn->y());
connect(open_btn, &QPushButton::clicked, this, &Widget::open_btn_slot);
//保存文件
save_btn = new QPushButton("保存文件", this);
save_btn->resize(90,40);
save_btn->move(open_btn->x()+120, font_btn->y());
connect(save_btn, &QPushButton::clicked, this, &Widget::save_btn_slot);
//清空文本框
clear_btn = new QPushButton("清空", this);
clear_btn->resize(90,40);
clear_btn->move(save_btn->x()+120, font_btn->y());
connect(clear_btn, &QPushButton::clicked, this, &Widget::clear_btn_slot);
}
Widget::~Widget()
{
delete ui;
}
//设置字体
void Widget::font_btn_slot()
{
bool ok; //判断用户是否选择字体
QFont font = QFontDialog::getFont(&ok, QFont("宋体", 12), this, "选择字体");
if(ok)
{
textedit->setFont(font);
}
}
//设置颜色
void Widget::color_btn_slot()
{
QColor color = QColorDialog::getColor(QColor(255,0,0), this, "选择颜色");
if(color.isValid())
{
//设置选中文本颜色
textedit->setTextColor(color);
}
}
//打开文件
void Widget::open_btn_slot()
{
QString pathName = QFileDialog::getOpenFileName(this, "打开文件", "./", "All(*.*);; Text(*.txt);; C/C++(*.c *.cpp)");
//判断用户是否选择文件
if(!pathName.isEmpty())
{
//实例化文件对象
QFile file(pathName);
//判断文件是否存在
if(file.exists())
{
//打开文件
file.open(QIODevice::ReadOnly);
//读取内容
QByteArray text = file.readAll();
textedit->setText(QString::fromLocal8Bit(text));
//关闭文件
file.close();
}
}
}
//保存文件
void Widget::save_btn_slot()
{
QString pathName = QFileDialog::getSaveFileName(this, "保存文件", "./", "TEXT(*.txt)");
//实例化文件对象
QFile file(pathName);
//打开文件
file.open(QIODevice::WriteOnly | QIODevice::Truncate);
//写入数据
QString text = textedit->toPlainText();
file.write(text.toLocal8Bit());
//关闭文件
file.close();
}
//清空文本框
void Widget::clear_btn_slot()
{
textedit->clear();
}
运行结果