一、实现段落对齐
完成对按下某个对齐按钮的响应函数,根据比较判断触发的是哪个对齐按钮,调用 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 ,也可根据需要进行其他设定。