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

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

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

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

(7)文档内容操作

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

cpp 复制代码
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);//对选中的字体格式进行设置
cpp 复制代码
void ChileWnd::setFormatOnSelectedWord(const QTextCharFormat &fmt)
{
    //获取文档光标
    QTextCursor tcursor = textCursor();
    if(!tcursor.hasSelection())
        tcursor.select(QTextCursor::WordUnderCursor);//选中模式
    tcursor.mergeCharFormat(fmt);
    //合并格式
    mergeCurrentCharFormat(fmt);
}
cpp 复制代码
//转到槽
void on_blodAction_triggered();

void on_inclineAction_triggered();

void on_underlineAction_triggered();
cpp 复制代码
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)信号,关于这几种功能,是需要对选中的文字进行设置

cpp 复制代码
void textFamily(const QString &fmly); //设置字体
void textSize(const QString &ps); //设置字号
cpp 复制代码
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)段落对齐

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

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

然后完成段落对齐的逻辑

cpp 复制代码
void setAlignOfDocumentText(int aligntype); //设置段落对齐方式
cpp 复制代码
//设置段落对齐方式
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);
    }
}
cpp 复制代码
    void on_leftAction_triggered();
    
    void on_rightAction_triggered();
    
    void on_centerAction_triggered();
    
    void on_justifyAction_triggered();
cpp 复制代码
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);
}
相关推荐
blasit20 小时前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
郑州光合科技余经理6 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1236 天前
matlab画图工具
开发语言·matlab
dustcell.6 天前
haproxy七层代理
java·开发语言·前端
norlan_jame6 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone6 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054966 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月6 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
m0_531237176 天前
C语言-数组练习进阶
c语言·开发语言·算法
Railshiqian6 天前
给android源码下的模拟器添加两个后排屏的修改
android·开发语言·javascript