Reputation: 485
I am using a QSyntaxHighlighter
to highlight a block of text in a QTextEdit
. The text looks like I would expect in the QTextEdit
on the display with the appropriate highlighting. If I then call QTextEdit::toHtml()
, the returned string does not include the highlighting colors that I am seeing in the QTextEdit
. Is there any way to get the actual highlighted text out as an html string?
Here is some sample code:
ScriptSyntaxHighlighter* scriptSyntaxHighlighter; //Implements QSyntaxHighlighter
QTextEdit* scriptTextEdit;
scriptTextEdit = new QTextEdit("//Here is a comment");
scriptSyntaxHighlighter = new ScriptSyntaxHighlighter(scriptTextEdit.document());
QString formattedText = scriptTextEdit.toHtml();
When I run the code above, the displayed QTextEdit shows a nicely colored comment. The html formatted formattedText
string, however, does not include any coloring tags.
Upvotes: 5
Views: 3919
Reputation: 76
Well, after some experimenting i manipulated some of the Qt Creator's code into something useful that you can use right in your QSyntaxHighlighter derived class. If you don't want to use any other default foreground and background colors in your document, skip the parts with tempCursor.setCharFormat() and blockFormat.setBackground(). This works just fine, so try it out.
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
Upvotes: 4
Reputation: 19112
You probably need to specify the encoding you want to use...
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 does it somehow (when you copy text from c++ editor and copy it into some other rich text editor, it gets the highlighting)...
The source of the cpp editor is here:
http://qt.gitorious.org/qt-creator/qt-creator/trees/master/src/plugins/cppeditor
I haven't found where Qt Creator does it yet in their source...
One way that you could do something like it, is to create your own html tags in the QSyntaxHighligher::highlightBlock()
and insert them into a copy of the text and store it separately.
http://qt-project.org/doc/qt-4.8/qsyntaxhighlighter.html#highlightBlock
Then when you need it exported, in your subclassed QSyntaxHighlighter
, you access that stored html text you generated.
Hope that helps.
Upvotes: 0