【问题标题】:How to indent multiline text behind lists in QMLs TextEdit如何在 QML 文本编辑中缩进列表后面的多行文本
【发布时间】:2016-02-25 23:48:21
【问题描述】:

在 QML 中,我将 TextEdit 项用于文本编辑器,并在其后面运行荧光笔代码 (QSyntaxHighlighter)。当用户键入破折号 (-) 时,它会被高亮代码识别并格式化(如 Markdown)。但另外,当它是多行时,我希望文本在破折号后面缩进。就像它处理 HTML 列表一样。

这就是现在的样子:

这就是我想要的(文本在破折号后面正确对齐):

我知道这可以缩进文本:

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

一个想法是默认缩进所有文本并用破折号或类似的方式取消缩进行,但还不能完全弄清楚如何实现它。

还有其他想法吗?

【问题讨论】:

    标签: list qt qml richtext textedit


    【解决方案1】:

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

    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 信号的插槽或荧光笔中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多