QSyntaxHighlighter does not change the character format directly in the QTextEdit. You cannot retrieve the format via QTextCursror::charFormat. QSyntaxHighlighter sets the format in additionalFormats as part of the block layout. You can get a list of all formats used in the block the cursor is in by using textCursor().block().layout()->additionalFormats(). This will be a list of FormatRange objects. Get the cursor position in the block using textCursor().positionInBlock() and loop over the items in additionalFormats checking if the cursor is within the item's range (the start of is relative within the block) and if the format matches what you're looking for.
Here is an excerpt from a project I'm working on where I'm checking the formatting set by QSyntaxHighlighter for offering spell checking suggestions.
int pos = c.positionInBlock();
foreach
(QTextLayout::FormatRange r, textCursor
().
block().
layout()->additionalFormats
()) { if (pos >
= r.
start && pos <
= r.
start + r.
length && r.
format.
underlineStyle() == QTextCharFormat::SpellCheckUnderline) { c.setPosition(c.block().position() + r.start);
setTextCursor(c);
offerSpelling = true;
break;
}
}
QTextCursor c = textCursor();
int pos = c.positionInBlock();
foreach (QTextLayout::FormatRange r, textCursor().block().layout()->additionalFormats()) {
if (pos >= r.start && pos <= r.start + r.length && r.format.underlineStyle() == QTextCharFormat::SpellCheckUnderline) {
c.setPosition(c.block().position() + r.start);
c.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, r.length);
setTextCursor(c);
offerSpelling = true;
break;
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks