如何将QSyntaxHighlighter中突出显示的文本转换为html字符串?

时间:2013-03-07 19:51:47

标签: html qt syntax-highlighting

我使用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字符串不包含任何着色标记。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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/cppeditor

我还没有找到Qt Creator在其来源中所做的事情......

你可以做一些类似的事情,就是在QSyntaxHighligher::highlightBlock()中创建自己的html标签,然后将它们插入文本的副本并单独存储。

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

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

希望有所帮助。