如何在QML TextEdit中缩进列表后面的多行文本

时间:2016-02-25 23:48:22

标签: list qt qml richtext textedit

在QML中我使用TextEdit项目作为文本编辑器,后面运行了高亮显示的代码(QSyntaxHighlighter)。当用户键入短划线( - )时,它将被突出显示的代码识别并格式化(如Markdown)。但另外我希望文本在短划线后面缩进,当它是多线时。就像它与HTML列表一样。

这就是现在的样子: How it looks like right now

这就是我想要的方式(文本在短划线后正确对齐): enter image description here

我知道这可以缩进文字:

QTextCursor cursor(currentBlock());
QTextBlockFormat textBlockFormat = currentBlock().blockFormat();
textBlockFormat.setIndent(1);
cursor.setBlockFormat(textBlockFormat);

一个想法是默认缩进所有文本并用短划线或类似方法缩进行,但还不能弄清楚如何实现它。

还有其他想法吗?

1 个答案:

答案 0 :(得分:0)

好吧显然还有一个列表样式选项。这是你可以改变块的样式的方法:

QTextBlock block = textDoc->findBlockByNumber(i);
QTextCursor cursor(block);

cursor.beginEditBlock();
QTextListFormat::Style style = QTextListFormat::ListDecimal;
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();

这可用于链接到QTextDocument或荧光笔中的contentsChange信号的插槽。