十九、【文本编辑器(五)】排版功能


目录

一、搭建框架

二、实现段落对齐

三、实现文本排序


一、搭建框架

(1) 在imgprocessor.h文件中添加private变量:

    QLabel *listLabel;                              //排序设置项
    QComboBox *listComboBox;
    QActionGroup *actGrp;
    QAction *leftAction;
    QAction *rightAction;
    QAction *centerAction;
    QAction *justifyAction;

    QToolBar *listToolBar;                          //排序工具栏

 (2) 在文件中添加protected slots函数:

    void ShowList(int);
    void ShowAlignment(QAction *act);
    void ShowCursorPositionChanged();

(3)  在ImgProcessor构造函数中,添加如下代码:

    //排序
    listLabel =new QLabel(tr("排序"));
    listComboBox =new QComboBox;
    listComboBox->addItem("Standard");
    listComboBox->addItem("QTextListFormat::ListDisc");
    listComboBox->addItem("QTextListFormat::ListCircle");
    listComboBox->addItem("QTextListFormat::ListSquare");
    listComboBox->addItem("QTextListFormat::ListDecimal");
    listComboBox->addItem("QTextListFormat::ListLowerAlpha");
    listComboBox->addItem("QTextListFormat::ListUpperAlpha");
    listComboBox->addItem("QTextListFormat::ListLowerRoman");
    listComboBox->addItem("QTextListFormat::ListUpperRoman");

(4) 在构造函数的最后部分添加相关的事件关联:

    connect(listComboBox,SIGNAL(activated(int)),this,SLOT(ShowList(int)));
    connect(showWidget->text->document(),SIGNAL(undoAvailable(bool)),redoAction,SLOT(setEnabled(bool)));
    connect(showWidget->text->document(),SIGNAL(redoAvailable(bool)),redoAction,SLOT(setEnabled(bool)));
    connect(showWidget->text,SIGNAL(cursorPositionChanged()),this,SLOT(ShowCursorPositionChanged()));

(5) 在相对应的工具栏 createActions 函数中添加如下代码:

    //排序:左对齐、右对齐、居中和两端对齐
    actGrp =new QActionGroup(this);

    leftAction =new QAction(QIcon("left.png"),"左对齐",actGrp);
    leftAction->setCheckable(true);

    rightAction =new QAction(QIcon("right.png"),"右对齐",actGrp);
    rightAction->setCheckable(true);

    centerAction =new QAction(QIcon("center.png"),"居中",actGrp);
    centerAction->setCheckable(true);

    justifyAction =new QAction(QIcon("justify.png"),"两端对齐",actGrp);
    justifyAction->setCheckable(true);

    connect(actGrp,SIGNAL(triggered(QAction*)),this,SLOT(ShowAlignment(QAction*)));

(6) 在相对应的工具栏 createToolBars()函数中添加如下代码:

    //排序工具条
    listToolBar =addToolBar("list");
    listToolBar->addWidget(listLabel);
    listToolBar->addWidget(listComboBox);
    listToolBar->addSeparator();                //添加一个分隔符
    listToolBar->addActions(actGrp->actions());

二、实现段落对齐

        完成对按下某个对齐按钮的响应用ShowAlignment()函数,根据比较判断触发的是哪个对齐按钮,调用 QTextEdit 的 setAlignment 函数可以实现当前段落的对齐调整。具体代码如下:

void ImgProcessor::ShowAlignment(QAction *act)
{
    if(act==leftAction)
        showWidget->text->setAlignment(Qt::AlignLeft);
    if(act==rightAction)
        showWidget->text->setAlignment(Qt::AlignRight);
    if(act==centerAction)
        showWidget->text->setAlignment(Qt::AlignCenter);
    if(act==justifyAction)
        showWidget->text->setAlignment(Qt::AlignJustify);
}

        响应文本中光标位置处发生改变的信号的ShowCursorPositionChanged()函数代码如下:

//显示光标处的对齐方式
void ImgProcessor::ShowCursorPositionChanged()
{
    if(showWidget->text->alignment()==Qt::AlignLeft)
        leftAction->setChecked(true);
    if(showWidget->text->alignment()==Qt::AlignRight)
        rightAction->setChecked(true);
    if(showWidget->text->alignment()==Qt::AlignCenter)
        centerAction->setChecked(true);
    if(showWidget->text->alignment()==Qt::AlignJustify)
        justifyAction->setChecked(true);
}

        完成四个对齐按钮的状态更新。通过调用 QTextEdit 类的alignment()函数获得当前光标所在处段落的对齐方式,设置相应的对齐按钮为按下状态。

三、实现文本排序

        文本排序功能实现的基本流程,如下图所示:

        

        主要用于描述文本排序格式的 QTextListFormat 包含两个基本的属性,一个为QTextListFormat::style , 表 示文本 采用哪 种排序 方式;另 一 个为QTextListFormat::indent, 表示排序后的缩进值。因此,若要实现文本排序的功能则只需设置好 QTextListFormat 的以上两个属性,并将整个格式通过 QTextCursor 类应用到文本中即可。 

void ImgProcessor::ShowList(int index)
{
    QTextCursor cursor=showWidget->text->textCursor();

    if(index!=0)
    {
        QTextListFormat::Style style=QTextListFormat::ListDisc;     //用于描述文本排序格式QTextListFormat
        switch(index)                                               //设置style属性值
        {
        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; break;
        }
        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);
    }
}

相关推荐

  1. 周周报

    2024-07-17 11:58:01       15 阅读
  2. 中介子方程

    2024-07-17 11:58:01       25 阅读
  3. React富文本编辑器开发(一)命令与编辑器

    2024-07-17 11:58:01       37 阅读
  4. React富文本编辑器开发()变换

    2024-07-17 11:58:01       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-17 11:58:01       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-17 11:58:01       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-17 11:58:01       58 阅读
  4. Python语言-面向对象

    2024-07-17 11:58:01       69 阅读

热门阅读

  1. electron中app.whenReady()和app.on(‘ready‘)的区别

    2024-07-17 11:58:01       22 阅读
  2. Scikit-Learn 基础教程

    2024-07-17 11:58:01       22 阅读
  3. 八部金刚功1.0.9-july 17th-冥想1.1.7

    2024-07-17 11:58:01       25 阅读
  4. docker-cli & nerdctl & ctr & crictl容器命令比较

    2024-07-17 11:58:01       17 阅读
  5. Django captcha 验证

    2024-07-17 11:58:01       23 阅读
  6. Django REST Framework(九)GenericAPIView视图子类

    2024-07-17 11:58:01       25 阅读
  7. 【C++】C语言和C++的区别

    2024-07-17 11:58:01       22 阅读
  8. Angular 开发编码规约

    2024-07-17 11:58:01       27 阅读
  9. 2407d,让d的printf安全

    2024-07-17 11:58:01       30 阅读
  10. 【python】python装饰器整理

    2024-07-17 11:58:01       20 阅读
  11. 金豺狼优化算法(GWO)及其Python和MATLAB实现

    2024-07-17 11:58:01       25 阅读
  12. ChatGPT等模型SQL优化提示词

    2024-07-17 11:58:01       24 阅读