简介
通过学习QT Widgets 实现的一个简易的文本编辑器,记录自己的学习。参考QT官方教程实现,以及修改官方教程的一些内容。
参考网址
https://zhuanlan.zhihu.com/p/378703210
源码解析
源码
main.cpp
#include "notepad.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Notepad w;
w.show();
return a.exec();
}
notepad.cpp
#include "notepad.h"
#include "ui_notepad.h"
#include <QFile>
#include <QFileDialog>
#include <QIODevice>
#include <QMessageBox>
#include <QTextStream>
#include <QCoreApplication>
#include <QPrinter>
#include <QPrintDialog>
#include <QFontDialog>
Notepad::Notepad(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::Notepad)
{
ui->setupUi(this);
//手动关联信号与槽
connect(ui->actionNew, &QAction::triggered, this, &Notepad::newDocument);
connect(ui->actionOpen, &QAction::triggered, this, &Notepad::open);
connect(ui->actionSave, &QAction::triggered, this, &Notepad::save);
connect(ui->actionSave_as, &QAction::triggered, this, &Notepad::saveAs);
connect(ui->actionPrint, &QAction::triggered, this, &Notepad::print);
connect(ui->actionexit, &QAction::triggered, this, &Notepad::exit);
connect(ui->actionCopy, &QAction::triggered, this, &Notepad::copy);
connect(ui->actionCut, &QAction::triggered, this, &Notepad::cut);
connect(ui->actionPaste, &QAction::triggered, this, &Notepad::paste);
connect(ui->actionUndo, &QAction::triggered, this, &Notepad::undo);
connect(ui->actionRedo, &QAction::triggered, this, &Notepad::redo);
connect(ui->actionFont, &QAction::triggered, this, &Notepad::selectFont);
connect(ui->actionBold, &QAction::triggered, this, &Notepad::setFontBold);
connect(ui->actionUnderline, &QAction::triggered, this, &Notepad::setFontUnderline);
connect(ui->actionItalic, &QAction::triggered, this, &Notepad::setFontItalic);
connect(ui->actionAbout, &QAction::triggered, this, &Notepad::about);
// Disable menu actions for unavailable features
#if !defined(QT_PRINTSUPPORT_LIB) || !QT_CONFIG(printer)
ui->actionPrint->setEnabled(false);
#endif
#if !QT_CONFIG(clipboard)
ui->actionCut->setEnabled(false);
ui->actionCopy->setEnabled(false);
ui->actionPaste->setEnabled(false);
#endif
}
Notepad::~Notepad()
{
delete ui;
}
void Notepad::newDocument()
{
ui->textEdit->clear();
ui->textEdit->setText(QString());
setWindowTitle("Notepad");//标题恢复
currentFile = "";//文件名恢复为空
}
void Notepad::open()
{
QString fileName = QFileDialog::getOpenFileName(this, "Open the file");
if (fileName.isEmpty()) return ;//用户没有选中一个文件
QFile file(fileName);
currentFile = fileName;
if(!file.open(QIODevice::ReadOnly | QFile::Text))//打开的文件不可读或者不是文本的话就报错
{
QMessageBox::warning(this, "Warning", "Cannot open the file :" + file.errorString());
return ;
}
setWindowTitle(fileName);
QTextStream in(&file);
QString text = in.readAll();
ui->textEdit->setText(text);
file.close();
}
void Notepad::save()
{
QString fileName;
if(currentFile.isEmpty())
{
fileName = QFileDialog::getSaveFileName(this, "Save");
if (fileName.isEmpty()) return ;
currentFile = fileName;
}
else
{
fileName = currentFile;
}
QFile file(fileName);
if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QMessageBox::warning(this, "Warning", "Cannot save file:" + file.errorString());
return ;
}
setWindowTitle(fileName);
QTextStream out(&file);
QString text = ui->textEdit->toPlainText();
out << text;
file.close();
}
void Notepad::saveAs()
{
QString fileName = QFileDialog::getSaveFileName(this, "Save as");
if (fileName.isEmpty()) return ;
QFile file(fileName);
if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QMessageBox::warning(this, "Warning", "Cannot save file:" + file.errorString());
return ;
}
currentFile = fileName;
setWindowTitle(fileName);
QTextStream out(&file);
QString text = ui->textEdit->toPlainText();
out << text;
file.close();
}
void Notepad::print()
{
#if defined (QT_PRINTSUPPORT_LIB) && QT_CONFIG(printer)
QPrinter printDev;
#if QT_CONFIG(printdialog)
QPrintDialog dialog(&printDev, this);
if(dialog.exec() == QDialog::Rejected) return ;
#endif /*QT_CONFIG(printdialog)*/
ui->textEdit->print(&printDev);
#endif /*QT_CONFIG(printer)*/
}
void Notepad::exit()
{
QCoreApplication::quit();
}
void Notepad::copy()
{
#if QT_CONFIG(clipboard)
ui->textEdit->copy();
#endif
}
void Notepad::cut()
{
#if QT_CONFIG(clipboard)
ui->textEdit->cut();
#endif
}
void Notepad::paste()
{
#if QT_CONFIG(clipboard)
ui->textEdit->paste();
#endif
}
void Notepad::undo()
{
ui->textEdit->undo();
}
void Notepad::redo()
{
ui->textEdit->redo();
}
void Notepad::selectFont()
{
bool fontSelected;
QFont font = QFontDialog::getFont(&fontSelected, this);
if(fontSelected)
{
ui->textEdit->setFont(font);
}
}
void Notepad::setFontUnderline()
{
QFont font = ui->textEdit->font();
font.setUnderline(!font.underline());//翻转效果
ui->textEdit->setFont(font);
}
void Notepad::setFontItalic()
{
QFont font = ui->textEdit->font();
font.setItalic(!font.italic());//翻转效果
ui->textEdit->setFont(font);
}
void Notepad::setFontBold()
{
QFont font = ui->textEdit->font();
font.setBold(!font.bold());//翻转效果
ui->textEdit->setFont(font);
}
void Notepad::about()
{
QMessageBox::about(this, tr("CCD_NOTOPAD_V1"),
tr("The <b>Notepad</b> example demonstrates how to code a basic "
"text editor using QtWidgets"));
}
notepad.h
#ifndef NOTEPAD_H
#define NOTEPAD_H
#include <QMainWindow>
#include <QString>
#define TEST 1
QT_BEGIN_NAMESPACE
namespace Ui { class Notepad; }
QT_END_NAMESPACE
class Notepad : public QMainWindow
{
Q_OBJECT
public:
explicit Notepad(QWidget *parent = nullptr);
~Notepad();
private slots:
//槽函数
void newDocument();
void open();
void save();
void saveAs();
void print();
void exit();
void copy();
void cut();
void paste();
void undo();
void redo();
void selectFont();
void setFontBold();
void setFontUnderline();
void setFontItalic();
void about();
private:
Ui::Notepad *ui;
QString currentFile = "";
};
#endif // NOTEPAD_H
qt_notebook_v1.pro
TEMPLATE = app
TARGET = notepad
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
qtHaveModule(printsupport): QT += printsupport
requires(qtConfig(fontdialog))
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
notepad.cpp
HEADERS += \
notepad.h
FORMS += \
notepad.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += \
resource.qrc
DISTFILES += \
icons/new_file .png
# install
target.path = $$[PWD]/exe
INSTALLS += target
修改的内容
如何添加资源文件
如果是第一次添加资源文件的话,右键工程文件夹,如右键下图qt_botebook_v1--->Add new。
接下来安装向导一步步操作即可。
如下图,创建完成之后,添加前缀/images 然后点击Add prefix。 然后点击Add Files 就可以添加我们想要的文件了。
如果是已经做好资源文件的向导创建,后续想再添加文件的话。
如上图,右键创建好的资源文件qrc ,打开资源编辑器,就可以出现Add file的界面。对于资源文件中的icons 可以去阿里巴巴矢量图标下载。网站如下:
打包程序
资源下载
百度网盘
链接:https://pan.baidu.com/s/1ZX5y-9oFKa4FgFBoW93Iig?pwd=1234
提取码:1234