【问题标题】:How can I get highlighted text from a QSyntaxHighlighter into an html string?如何将 QSyntaxHighlighter 中的突出显示文本转换为 html 字符串?
【发布时间】:2013-03-07 19:51:47
【问题描述】:

我正在使用QSyntaxHighlighter 突出显示QTextEdit 中的一段文本。文本看起来像我期望在显示器上的QTextEdit 中具有适当的突出显示。如果我随后调用QTextEdit::toHtml(),则返回的字符串不包括我在QTextEdit 中看到的突出显示颜色。有什么方法可以将实际突出显示的文本作为 html 字符串输出?

这里是一些示例代码:

ScriptSyntaxHighlighter* scriptSyntaxHighlighter; //Implements QSyntaxHighlighter
QTextEdit* scriptTextEdit;
scriptTextEdit = new QTextEdit("//Here is a comment");
scriptSyntaxHighlighter = new ScriptSyntaxHighlighter(scriptTextEdit.document());
QString formattedText = scriptTextEdit.toHtml();

当我运行上面的代码时,显示的 QTextEdit 显示了一个漂亮的颜色注释。但是,html 格式的formattedText 字符串不包含任何着色标签。

【问题讨论】:

    标签: html qt syntax-highlighting


    【解决方案1】:

    好吧,经过一些试验,我将 Qt Creator 的一些代码操作成有用的东西,您可以在 QSyntaxHighlighter 派生类中直接使用。 如果您不想在文档中使用任何其他默认前景色和背景色,请跳过带有 tempCursor.setCharFormat() 和 blockFormat.setBackground() 的部分。 这很好用,所以试试吧。

    void MyHighlighter::asHtml(QString& html)
    {
        // Create a new document from all the selected text document.
        QTextCursor cursor(document());
        cursor.select(QTextCursor::Document);
        QTextDocument* tempDocument(new QTextDocument);
        Q_ASSERT(tempDocument);
        QTextCursor tempCursor(tempDocument);
    
        tempCursor.insertFragment(cursor.selection());
        tempCursor.select(QTextCursor::Document);
        // Set the default foreground for the inserted characters.
        QTextCharFormat textfmt = tempCursor.charFormat();
        textfmt.setForeground(Qt::gray);
        tempCursor.setCharFormat(textfmt);
    
        // Apply the additional formats set by the syntax highlighter
        QTextBlock start = document()->findBlock(cursor.selectionStart());
        QTextBlock end = document()->findBlock(cursor.selectionEnd());
        end = end.next();
        const int selectionStart = cursor.selectionStart();
        const int endOfDocument = tempDocument->characterCount() - 1;
        for(QTextBlock current = start; current.isValid() and current not_eq end; current = current.next()) {
            const QTextLayout* layout(current.layout());
    
            foreach(const QTextLayout::FormatRange &range, layout->additionalFormats()) {
                const int start = current.position() + range.start - selectionStart;
                const int end = start + range.length;
                if(end <= 0 or start >= endOfDocument)
                    continue;
                tempCursor.setPosition(qMax(start, 0));
                tempCursor.setPosition(qMin(end, endOfDocument), QTextCursor::KeepAnchor);
                tempCursor.setCharFormat(range.format);
            }
        }
    
        // Reset the user states since they are not interesting
        for(QTextBlock block = tempDocument->begin(); block.isValid(); block = block.next())
            block.setUserState(-1);
    
        // Make sure the text appears pre-formatted, and set the background we want.
        tempCursor.select(QTextCursor::Document);
        QTextBlockFormat blockFormat = tempCursor.blockFormat();
        blockFormat.setNonBreakableLines(true);
        blockFormat.setBackground(Qt::black);
        tempCursor.setBlockFormat(blockFormat);
    
        // Finally retreive the syntax higlighted and formatted html.
        html = tempCursor.selection().toHtml();
        delete tempDocument;
    } // asHtml
    

    【讨论】:

    • @Larswad 我希望可以在 GPLv3 程序中使用此代码?
    【解决方案2】:

    您可能需要指定要使用的编码...

    http://qt-project.org/doc/qt-4.8/qtextdocument.html#toHtml

    http://qt-project.org/doc/qt-4.8/richtext-html-subset.html

    Qt Creator 以某种方式做到了(当您从 c++ 编辑器复制文本并将其复制到其他富文本编辑器时,它会突出显示)...

    cpp编辑器的源码在这里:

    http://qt.gitorious.org/qt-creator/qt-creator/trees/master/src/plugins/cppeditor

    我还没有在他们的源代码中找到 Qt Creator 在哪里......

    您可以做类似的事情的一种方法是在QSyntaxHighligher::highlightBlock() 中创建您自己的 html 标记并将它们插入到文本的副本中并单独存储。

    http://qt-project.org/doc/qt-4.8/qsyntaxhighlighter.html#highlightBlock

    然后,当您需要将其导出时,在您的子类 QSyntaxHighlighter 中,您可以访问您生成的存储的 html 文本。

    希望对您有所帮助。

    【讨论】:

    • 在我看来,编码参数本质上是在指定哪些字符集可用。 Qt 示例是“utf-8”,它是 unicode 字符的可变宽度编码。这将如何告诉系统添加颜色标签?
    • 为了确定,我手动将“utf-8”设置为函数中的编码,但没有任何区别。
    • 所以看起来并不容易。 Qt Creator 做了类似的事情(如果你复制它的语法高亮文本并将其粘贴到另一个富文本区域,它会突出显示)。这是其中包含荧光笔的来源:qt.gitorious.org/qt-creator/qt-creator/trees/master/src/plugins/…我还没有找到他们是如何做到的,但这是可能的。
    猜你喜欢
    • 2019-09-13
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    • 2014-03-12
    • 2022-01-11
    相关资源
    最近更新 更多