QT5开发及实例学习之十四Qt5排版功能


一、实现段落对齐

  完成对按下某个对齐按钮的响应函数,根据比较判断触发的是哪个对齐按钮,调用 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);
	}

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

	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 类应用到文本中即可。在这里插入图片描述
  在通常的文本编辑器中,QTextListFormat 的缩进值 indent 都是预设好的,并不需要由用户设定。
  本实例采用在程序中通过获取当前文本段 QTextBlockFormat 的缩进值来进行相应的计算的方法,以获得排序文本的缩进值。
  实现根据用户选择的不同排序方式对文本进行排序的函数代码如下:

	void ImgProcessor::showList(int index)
	{
	    //获得编辑框 QTextCursor 对象指针
	    QTextCursor cursor = showWidget->text->textCursor();
	    if(index != 0)
	    {
	        QTextListFormat::Style style = QTextListFormat::ListDisc;	//(a)
	        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;
	        }
	        /*设置缩进值*/												//(b)
	        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);
	    }
	}

(a)从下拉列表框中选择确定 QTextListFormat 的 style 属性值。Qt 提供了 8 种文本排序的方式,分别是 QTextListFormat::ListDisc、QTextListFormat::ListCircle、QTextListFormat::ListSquare、QTextListFormat::ListDecimal、QTextListFormat::ListLowerAlpha、QTextListFormat::ListUpperAlpha、QTextListFormat::ListLowerRoman 和 QTextListFormat::ListUpperRoman。
(b) cursor.beginEditBlock();
   …
   cursor.endEditBlock();
  此段代码完成 QTextListFormat 的另一个属性 indent (即缩进值)的设定,并将设置的格式应用到光标所在的文本处。
  以 cursor.beginEditBlock() 开始,以 cursor.endEditBlock() 结束,这两个函数的作用是设定这两个函数之间的所有操作相当于一个动作。如果需要进行撤销或恢复,则这两个函数之间的所有操作将同时被撤销或恢复,这两个函数通常成对出现。
  设置 QTextListFormat 的缩进值首先通过 QTextCursor 获得 QTextBlockFormat 对象,由 QTextBlockFormat 获得段落的缩进值,在此基础上定义 QTextListFormat 的缩进值,本实例是在段落缩进的基础上加 1 ,也可根据需要进行其他设定。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值