Qt项目——文本编辑器(功能模块③)

devtools/2024/10/19 3:26:21/

项目地址:GitHub - Outlier9/CatEditor: Cat文本编辑器--Qt

有帮助的话各位点点 star 啦,感谢!

如果有需要学习该项目的人,觉得看文档较为困难,可以加我联系方式,给github点个star后可免费提供学习视频!!!

(7)文档内容操作

文档内容的剪切、复制、粘贴,撤销(上一步)、重写(下一步)等操作,同上,在.ui界面对这些Action转到槽,选triggered信号,关于这几种功能,在Qt中有已实现的函数,直接调用即可

void MainWindow::on_undoAction_triggered()
{docUndo();
}void MainWindow::on_redoAction_triggered()
{docRedo();
}void MainWindow::on_cutAction_triggered()
{docCut();
}void MainWindow::on_copyAction_triggered()
{docCopy();
}void MainWindow::on_pasteAction_triggered()
{docPaste();
}//撤销(上一步)
void MainWindow::docUndo()
{if(activateChildWnd())activateChildWnd()->undo();
}//重写(下一步)
void MainWindow::docRedo()
{if(activateChildWnd())activateChildWnd()->redo();
}//剪切
void MainWindow::docCut()
{if(activateChildWnd())activateChildWnd()->cut();
}//复制
void MainWindow::docCopy()
{if(activateChildWnd())activateChildWnd()->copy();
}//粘贴
void MainWindow::docPaste()
{if(activateChildWnd())activateChildWnd()->paste();
}

(8)字体格式

文字的加粗、倾斜、下划线操作,同上,在.ui界面对这些Action转到槽,选triggered信号,关于这几种功能,是需要对选中的文字进行设置

void setFormatOnSelectedWord(const QTextCharFormat &fmt);//对选中的字体格式进行设置
void ChileWnd::setFormatOnSelectedWord(const QTextCharFormat &fmt)
{//获取文档光标QTextCursor tcursor = textCursor();if(!tcursor.hasSelection())tcursor.select(QTextCursor::WordUnderCursor);//选中模式tcursor.mergeCharFormat(fmt);//合并格式mergeCurrentCharFormat(fmt);
}
//转到槽
void on_blodAction_triggered();void on_inclineAction_triggered();void on_underlineAction_triggered();
void MainWindow::on_blodAction_triggered()
{textBold();
}void MainWindow::on_inclineAction_triggered()
{textItalic();
}void MainWindow::on_underlineAction_triggered()
{textUnderline();
}//加粗
void MainWindow::textBold()
{QTextCharFormat fmt;fmt.setFontWeight(ui->blodAction->isChecked() ? QFont::Bold : QFont::Normal);if(activateChildWnd())activateChildWnd()->setFormatOnSelectedWord(fmt);
}//倾斜
void MainWindow::textItalic()
{QTextCharFormat fmt;fmt.setFontItalic(ui->inclineAction->isChecked());if(activateChildWnd())activateChildWnd()->setFormatOnSelectedWord(fmt);
}//下划线
void MainWindow::textUnderline()
{QTextCharFormat fmt;fmt.setFontUnderline(ui->underlineAction->isChecked());if(activateChildWnd())activateChildWnd()->setFormatOnSelectedWord(fmt);
}

在该功能实现中,对字体的设置是对选中的字体进行设置,所以代码在isChecked这一步,需要确保该Action是可勾选的,也就是Checkable

(9)字号字体

文字设置字体和字号操作,在.ui界面对这些控件转到槽,选activated(QString)信号,关于这几种功能,是需要对选中的文字进行设置

void textFamily(const QString &fmly); //设置字体
void textSize(const QString &ps); //设置字号
void MainWindow::on_fontComboBox_activated(const QString &arg1)
{textFamily(arg1);
}void MainWindow::on_sizeComboBox_activated(const QString &arg1)
{textSize(arg1);
}//设置字体
void MainWindow::textFamily(const QString &fmly)
{QTextCharFormat fmt;fmt.setFontFamily(fmly);if(activateChildWnd())activateChildWnd()->setFormatOnSelectedWord(fmt);
}//设置字号
void MainWindow::textSize(const QString &ps)
{qreal pointSize = ps.toFloat();if(ps.toFloat() > 0){QTextCharFormat fmt;fmt.setFontPointSize(pointSize);if(activateChildWnd())activateChildWnd()->setFormatOnSelectedWord(fmt);}
}

 (10)段落对齐

对齐方式有左端对齐、居中对齐、右端对齐、两端对齐,同时只能存在一种,所以这四种互斥,需要在初始化中设置互斥性

//对齐方式互斥性,一次只能选一种QActionGroup *alignGroup = new QActionGroup(this);alignGroup->addAction(ui->leftAction);alignGroup->addAction(ui->rightAction);alignGroup->addAction(ui->centerAction);alignGroup->addAction(ui->justifyAction);

然后完成段落对齐的逻辑

void setAlignOfDocumentText(int aligntype); //设置段落对齐方式
//设置段落对齐方式
void ChileWnd::setAlignOfDocumentText(int aligntype)
{//给传入的参数设置编号,1-->左端对齐,2-->右端对齐,3-->居中对齐,4-->两端对齐if(aligntype == 1){setAlignment(Qt::AlignLeft | Qt::AlignAbsolute);}else if(aligntype == 2){setAlignment(Qt::AlignRight | Qt::AlignAbsolute);}else if(aligntype == 3){setAlignment(Qt::AlignCenter);}else if(aligntype == 4){setAlignment(Qt::AlignJustify);}
}
    void on_leftAction_triggered();void on_rightAction_triggered();void on_centerAction_triggered();void on_justifyAction_triggered();
void MainWindow::on_leftAction_triggered()
{if(activateChildWnd())activateChildWnd()->setAlignOfDocumentText(1);
}void MainWindow::on_rightAction_triggered()
{if(activateChildWnd())activateChildWnd()->setAlignOfDocumentText(2);
}void MainWindow::on_centerAction_triggered()
{if(activateChildWnd())activateChildWnd()->setAlignOfDocumentText(3);
}void MainWindow::on_justifyAction_triggered()
{if(activateChildWnd())activateChildWnd()->setAlignOfDocumentText(4);
}


http://www.ppmy.cn/devtools/88245.html

相关文章

MuseTalk - 数字人唇部同步

文章目录 一、关于 MuseTalk概览新闻模型案例待办事项:第三方集成 二、安装构建环境mmlab 软件包下载 ffmpeg-static下载权重 三、快速入门推理使用 bbox_shift 以获得可调整的结果结合 MuseV 和 MuseTalk🆕实时推理实时推理注意事项 四、其它致谢限制引…

仓颉语言 -- 网络编程

使用新版本 (2024-07-19 16:10发布的) 1、网络编程概述 网络通信是两个设备通过计算机网络进行数据交换的过程。通过编写软件达成网络通信的行为即为网络编程。 仓颉为开发者提供了基础的网络编程功能,在仓颉标准库中,用户可使用…

搭建日志系统ELK(二)

搭建日志系统ELK(二) 架构设计 在搭建以ELK为核心的日志系统时,Logstash作为日志采集的核心组件,负责将各个服务的日志数据采集、清洗、过滤。然而缺点也很明显: 占用较多的服务器资源。配置复杂,学习曲线陡峭。处理大数据量时…

【unity小技巧】unity性能优化以及如何进行性能测试

文章目录 前言GPU性能优化打包素材 CPU性能优化代码执行优化 性能测试Vector2.Distance 和 sqrMagnitude哪个好?动画切换优化shader属性优化 URP渲染器资产优化对象池优化删除没必要的空函数图片、音乐音效、贴图等素材压缩ScriptableObject优化参数参考完结 前言 …

基于域名、ip的虚拟主机

一、基于域名的虚拟主机 基于域名的虚拟主机配置通常涉及服务器配置,特别是在使用Web服务器(如Apache, Nginx等)时。这里,我将提供两种流行的Web服务器(Apache和Nginx)的基本配置示例,用于基于…

【Linux】:环境变量

目录 前言(进程的特性): 1.认识环境变量 1.1 PATH 1.2 HOME 1.3 SHELL 1.4 HISSIZE 1.5 SSH_TTY 1.6 PWD和OLPWD 2. 环境变量的概念及其相关指令 什么是环境变量 echo(输出字符串到终端) env&#xff0…

嵌入式单片机中在线调试工具使用方法

大家好,相信很多小伙伴都听说过,或者用过SystemView这款工具。 它是一个可以在线调试嵌入式系统的工具,它可以分析RTOS有哪些中断、任务执行了,以及这些中断、任务执行的先后关系。 还可以查看一些内核对象持有和释放的时间点,比如信号量、互斥量、事件、消息队列等,这在…

Python笔试面试题AI答之面向对象(2)

文章目录 6.阐述 Python自省(机制与函数) ?7.简述Python中面向切面编程AOP和装饰器?面向切面编程(AOP)基本概念核心原理应用场景Python中的实现方式 装饰器(Decorator)基本概念语法应…