gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 12:50:42 25 4
gpt4 key购买 nike

我正在使用 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

关于html - 如何将突出显示的文本从 QSyntaxHighlighter 转换为 html 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15280452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com