簡體   English   中英

如何將QSyntaxHighlighter中突出顯示的文本轉換為html字符串?

[英]How can I get highlighted text from a QSyntaxHighlighter into an html string?

我正在使用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字符串不包含任何着色標記。

好吧,在經過一些實驗之后,我將一些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

您可能需要指定要使用的編碼...

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文本。

希望有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM