4

我正在使用 aQSyntaxHighlighter突出显示 a 中的一段文本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字符串不包含任何着色标签。

4

2 回答 2

4

好吧,经过一些实验后,我将 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
于 2013-04-04T10:40:57.460 回答
0

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

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/cpeditor

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

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

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

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

希望有帮助。

于 2013-03-07T20:06:15.650 回答