1 Star 3 Fork 0

clown/文本编辑器QT

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
mainwindow.cpp 14.70 KB
一键复制 编辑 原始数据 按行查看 历史
clown 提交于 2021-06-10 10:25 . "修改缩放"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QTextStream>
#include <QPrinter>
#include <QPrintDialog>
#include <QPainter>
#include <QTextCursor>
#include <QColorDialog>
#include <QTextList>
#include <QTextListFormat>
#include <QTextBlockFormat>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle("文本编辑器");
setWindowIcon(QIcon(":/icons/pkq.ico"));
init();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::init()
{
showWidget = new ShowWidget(this); //创建显示窗口
setCentralWidget(showWidget); //设置showWidget为中心窗体
fontLabel1 = new QLabel(tr("字体:"));
fontComboBox = new QFontComboBox;
fontComboBox->setMaximumWidth(120);
fontComboBox->setFontFilters(QFontComboBox::ScalableFonts);
fontLabel2 = new QLabel(tr("字号: "));
sizeComboBox = new QComboBox;
QFontDatabase db;
foreach(int size, db.standardSizes())
{
sizeComboBox->addItem(QString::number(size));
}
boldBtn = new QToolButton;
boldBtn->setIcon(QIcon(":/icons/blod.ico"));
boldBtn->setCheckable(true);
italicBtn = new QToolButton;
italicBtn->setIcon(QIcon(":/icons/italic.ico"));
italicBtn->setCheckable(true);
underlineBtn = new QToolButton;
underlineBtn->setIcon(QIcon(":/icons/underline.ico"));
underlineBtn->setCheckable(true);
colorBtn = new QToolButton;
colorBtn->setIcon(QIcon(":/icons/Q15.ico"));
colorBtn->setCheckable(true);
//文字排版功能
listLabel = new QLabel(tr("排序"));
listComboBox = new QComboBox;
listComboBox->setMaximumWidth(120);
listComboBox->addItems(fontFormatLists);
createActions(); //创建动作
createToolBars(); //创建工具栏
//加载图片
if(img.load(":/images/pkc.png"))
{
showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
}
//各字体设置相关功能的信号与槽
connect(fontComboBox, &QFontComboBox::textActivated, this, &MainWindow::showFontComboBox);
connect(sizeComboBox, &QComboBox::textActivated, this, &MainWindow::fontSizeSpinBox);
connect(boldBtn, &QToolButton::clicked, this, &MainWindow::fontBold);
connect(italicBtn, &QToolButton::clicked, this, &MainWindow::fontItalic);
connect(underlineBtn, &QToolButton::clicked, this, &MainWindow::fontUnderline);
connect(colorBtn, &QToolButton::clicked, this, &MainWindow::fontColor);
//文字排版功能的信号与槽
connect(listComboBox, SIGNAL(activated(int)), this, SLOT(showList(int)));
connect(showWidget->text->document(), &QTextDocument::undoAvailable , ui->action_undo, &QAction::setEnabled);
connect(showWidget->text->document(), &QTextDocument::redoAvailable, ui->action_redo, &QAction::setEnabled);
connect(showWidget->text, &QTextEdit::cursorPositionChanged, this, &MainWindow::showCursorPositionChanged);
}
void MainWindow::createActions()
{
//文字排版功能
actGrp = new QActionGroup(this);
leftAct = new QAction(QIcon(":/icons/leftalign.ico"),"左对齐" ,actGrp);
leftAct->setCheckable(true);
rightAct = new QAction(QIcon(":/icons/rightalign.ico") , "右对齐" ,actGrp);
rightAct->setCheckable(true);
centerAct = new QAction(QIcon(":/icons/centeralign.ico") , "居中" ,actGrp);
centerAct->setCheckable(true);
justifyAct = new QAction(QIcon(":/icons/adjustalign.ico") , "两端对齐" , actGrp);
justifyAct->setCheckable(true);
//连接信号槽
connect(ui->action_openFile, &QAction::triggered, this, &MainWindow::openFile);
connect(ui->action_newFile, &QAction::triggered, this, &MainWindow::newFile);
connect(ui->action_printText, &QAction::triggered, this, &MainWindow::printText);
connect(ui->action_printImage, &QAction::triggered, this, &MainWindow::printImage);
connect(ui->action_exit, &QAction::triggered, showWidget->text, &ShowWidget::close);
connect(ui->action_copy, &QAction::triggered, showWidget->text, &QTextEdit::copy);
connect(ui->action_paste, &QAction::triggered, showWidget->text, &QTextEdit::paste);
connect(ui->action_cut, &QAction::triggered, showWidget->text, &QTextEdit::cut);
connect(ui->action_zoomIn, &QAction::triggered, this, &MainWindow::zoomIn);
connect(ui->action_zoomOut, &QAction::triggered, this, &MainWindow::zoomOut);
connect(ui->action_about, &QAction::triggered, qApp, &QApplication::aboutQt);
connect(ui->action_rorate90, &QAction::triggered, this, &MainWindow::rotate90);
connect(ui->action_rorate180, &QAction::triggered, this, &MainWindow::rotate180);
connect(ui->action_rorate270, &QAction::triggered, this, &MainWindow::rotate270);
connect(ui->action_vertical, &QAction::triggered, this, &MainWindow::mirrorVertical);
connect(ui->action_horizantal, &QAction::triggered, this, &MainWindow::mirrorHorizontal);
connect(ui->action_undo, &QAction::triggered, showWidget->text, &QTextEdit::undo);
connect(ui->action_redo, &QAction::triggered, showWidget->text, &QTextEdit::redo);
connect(actGrp, &QActionGroup::triggered, this, &MainWindow::showAlignment);
}
void MainWindow::createToolBars() //创建工具栏
{
fileTool = addToolBar("File"); //创建文件工具栏
fileTool->addAction(ui->action_openFile);
fileTool->addAction(ui->action_newFile);
fileTool->addAction(ui->action_printText);
fileTool->addAction(ui->action_printImage);
editTool = addToolBar("Edit"); //创建文字编辑工具栏
editTool->addAction(ui->action_copy);
editTool->addAction(ui->action_cut);
editTool->addAction(ui->action_paste);
editTool->addSeparator();
editTool->addAction(ui->action_zoomIn);
editTool->addAction(ui->action_zoomOut);
rotateTool = addToolBar("Rotate"); //创建旋转工具栏
rotateTool->addAction(ui->action_rorate90);
rotateTool->addAction(ui->action_rorate180);
rotateTool->addAction(ui->action_rorate270);
doToolBar = addToolBar("DoEdit"); //创建撤销和重做工具栏
doToolBar->addAction(ui->action_undo);
doToolBar->addAction(ui->action_redo);
fontToolBar = addToolBar("Font"); //创建字体工具栏
fontToolBar->addWidget(fontLabel1);
fontToolBar->addWidget(fontComboBox);
fontToolBar->addWidget(fontLabel2);
fontToolBar->addWidget(sizeComboBox);
fontToolBar->addSeparator(); //加入分割线
fontToolBar->addWidget(boldBtn);
fontToolBar->addWidget(italicBtn);
fontToolBar->addWidget(underlineBtn);
fontToolBar->addSeparator();
fontToolBar->addWidget(colorBtn);
listToolBar = addToolBar("list"); //创建文字排版工具栏
listToolBar->addWidget(listLabel);
listToolBar->addWidget(listComboBox);
listToolBar->addSeparator();
listToolBar->addActions(actGrp->actions());
}
void MainWindow::newFile() //新建文件操作
{
MainWindow *newMainWin = new MainWindow;
newMainWin->show();
}
void MainWindow::openFile() //读文本是打开新窗口
{
fileName = QFileDialog::getOpenFileName(this);
if(!fileName.isEmpty()) //文件不为空
{
if(showWidget->text->document()->isEmpty()) //如果显示窗口为空
{
loadFile(fileName); //加载文件
}
else
{
MainWindow *newMainWin = new MainWindow;
newMainWin->show(); //当一个窗口已经打开了一个文件,还需要再打开文件时,新生成窗口
newMainWin->loadFile(fileName); //新窗口加载文件
}
}
}
void MainWindow::loadFile(QString filename) //载入文件
{
qDebug()<<"File name:%s\n"<<filename.data();
QFile file(filename); //创建文件
if(file.open(QIODevice::ReadOnly | QIODevice::Text)) //打开文件
{
QTextStream textStream(&file); //使用文本流读取文件
while(!textStream.atEnd()) //是否读取完
{
showWidget->text->append(textStream.readLine()); //读取下一行
qDebug()<<"Read line\n";
}
qDebug()<<"End\\n";
}
}
void MainWindow::printText() //打印文本
{
QPrinter printer;
QPrintDialog printDialog;
if(printDialog.exec())
{
QTextDocument *doc = showWidget->text->document();
qDebug()<<"doc %s"<<doc->toPlainText();
// doc->print(printer);
}
}
void MainWindow::printImage() //打印图片
{
QPrinter printer;
QPrintDialog printDialog;
if(printDialog.exec())
{
QPainter painter(&printer);
QRect rect = painter.viewport();
QSize size = img.size();
size.scale(rect.size() , Qt::KeepAspectRatio);
painter.setViewport(rect.x() , rect.y(), size.width(), size.height());
painter.setWindow(img.rect());
painter.drawImage(0,0,img);
}
}
void MainWindow::zoom(qreal sx, qreal sy)
{
if(img.isNull()) return;
QSize size(img.width() * sx, img.height() * sy);
img = img.scaled(size);
showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
}
void MainWindow::zoomIn() //图片放大
{
zoom(2, 2);
}
void MainWindow::zoomOut() //图片缩小
{
zoom(0.5, 0.5);
}
void MainWindow::rotateImg(qreal degree)//旋转图片功能
{
if(img.isNull()) return; //不存在图片则返回
QMatrix matrix;
matrix.rotate(degree);
QTransform transForm(matrix);
img = img.transformed(transForm);
showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
}
void MainWindow::rotate90() //旋转图片功能
{
rotateImg(90);
}
void MainWindow::rotate180()
{
rotateImg(180);
}
void MainWindow::rotate270()
{
rotateImg(270);
}
void MainWindow::mirrorFlip(bool horiz, bool vert)
{
if(img.isNull()) return;
img = img.mirrored(horiz, vert);
showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
}
void MainWindow::mirrorHorizontal() //水平镜像, 图像水平翻转
{
mirrorFlip(true, false);
}
void MainWindow::mirrorVertical() //垂直镜像, 图像垂直翻转
{
mirrorFlip(false, true);
}
void MainWindow::showFontComboBox(QString comboStr) //设置字体
{
QTextCharFormat fmt;
fmt.setFontFamily(comboStr);
mergeFormat(fmt);
}
void MainWindow::mergeFormat(QTextCharFormat format)
{
QTextCursor cursor = showWidget->text->textCursor();
if(!cursor.hasSelection())
{
cursor.select(QTextCursor::WordUnderCursor);
}
cursor.mergeCharFormat(format);
showWidget->text->mergeCurrentCharFormat(format);
}
void MainWindow::fontSizeSpinBox(QString spinvalue) //设置字号
{
QTextCharFormat fmt;
fmt.setFontPointSize(spinvalue.toFloat());
showWidget->text->mergeCurrentCharFormat(fmt);
}
void MainWindow::fontBold() //设置文字加粗
{
QTextCharFormat fmt;
fmt.setFontWeight(boldBtn->isCheckable() ? QFont::Bold : QFont::Normal);
showWidget->text->mergeCurrentCharFormat(fmt);
}
void MainWindow::fontItalic()
{
QTextCharFormat fmt;
fmt.setFontItalic(italicBtn->isCheckable());
showWidget->text->mergeCurrentCharFormat(fmt);
}
void MainWindow::fontUnderline()
{
QTextCharFormat fmt;
fmt.setFontUnderline(underlineBtn->isCheckable());
showWidget->text->mergeCurrentCharFormat(fmt);
}
void MainWindow::fontColor()
{
QColor color = QColorDialog::getColor(Qt::red, this);
if(color.isValid())
{
QTextCharFormat fmt;
fmt.setForeground(color);
showWidget->text->mergeCurrentCharFormat(fmt);
}
}
void MainWindow::fontCurFormatChanged(const QTextCharFormat &fmt)
{
fontComboBox->setCurrentIndex(fontComboBox->findText(fmt.fontFamily()));
sizeComboBox->setCurrentIndex(sizeComboBox->findText(QString::number(fmt.fontPointSize())));
boldBtn->setCheckable(fmt.font().bold());
italicBtn->setCheckable(fmt.fontItalic());
underlineBtn->setCheckable(fmt.fontUnderline());
}
void MainWindow::showAlignment(QAction *act)
{
if(act == leftAct)
{
showWidget->text->setAlignment(Qt::AlignLeft);
}
if(act == rightAct)
{
showWidget->text->setAlignment(Qt::AlignRight);
}
if(act == centerAct)
{
showWidget->text->setAlignment(Qt::AlignCenter);
}
if(act == justifyAct)
{
showWidget->text->setAlignment(Qt::AlignJustify);
}
}
void MainWindow::showCursorPositionChanged()
{
if(showWidget->text->alignment() == Qt::AlignLeft)
{
leftAct->setCheckable(true);
}
if(showWidget->text->alignment() == Qt::AlignRight)
{
rightAct->setCheckable(true);
}
if(showWidget->text->alignment() == Qt::AlignCenter)
{
centerAct->setCheckable(true);
}
if(showWidget->text->alignment() == Qt::AlignJustify)
{
justifyAct->setCheckable(true);
}
}
void MainWindow::showList(int index)
{
QTextCursor cursor = showWidget->text->textCursor();
if(index != 0)
{
QTextListFormat::Style style = QTextListFormat::ListDisc;
switch(index)
{
default:
case 1 :
style = QTextListFormat::ListDisc;
break;
case 2:
style = QTextListFormat::ListCircle;
break;
case 3:
style = QTextListFormat::ListSquare;
break;
case 4:
style = QTextListFormat::ListDecimal;
break;
case 5:
style = QTextListFormat::ListLowerAlpha;
break;
case 6:
style = QTextListFormat::ListUpperAlpha;
break;
case 7:
style = QTextListFormat::ListLowerRoman;
break;
case 8:
style = QTextListFormat::ListUpperRoman;
}
cursor.beginEditBlock(); //设置缩进值
QTextBlockFormat blockFmt = cursor.blockFormat();
QTextListFormat listFmt;
if(cursor.currentList())
{
listFmt = cursor.currentList()->format();
}
else
{
listFmt.setIndent(blockFmt.indent() + 1);
blockFmt.setIndent(0);
cursor.setBlockFormat(blockFmt);
}
listFmt.setStyle(style);
cursor.createList(listFmt);
cursor.endEditBlock();
}
else
{
QTextBlockFormat bfmt;
bfmt.setObjectIndex(-1);
cursor.mergeBlockFormat(bfmt);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/roc_kun/text-editor-qt.git
git@gitee.com:roc_kun/text-editor-qt.git
roc_kun
text-editor-qt
文本编辑器QT
master

搜索帮助