Reputation: 51
Sorry if this is a newbie question. I have some code for marking errors in an editor and I can't find how to clear the markings when the errors are gone. Here's the method I'm calling for rendering the errors.
void Editor::highlightErrors( Thing* t )
{
if ( !t )
return;
const std::vector<Thing::Error>& errors = t->errors();
QTextCursor tc = textCursor();
tc.select(QTextCursor::Document);
QList<QTextEdit::ExtraSelection> extraSelections;
for(int i = 0; i < errors.size(); ++i) {
const Thing::Error& error = errors[i];
QTextEdit::ExtraSelection sel;
sel.format = this->errorFormat();
sel.format.setToolTip(QString(error.error.c_str()));
sel.cursor = tc;
sel.cursor.clearSelection();
sel.cursor.setPosition(error.startPos);
sel.cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, error.endPos - error.startPos);
sel.cursor.mergeCharFormat(sel.format);
extraSelections.append(sel);
}
setExtraSelections(extraSelections);
}
I would expect QPlainTextEdit to redraw the text without the error format, when this method is called with no errors, but it doesn't. QTextLayout::draw is properly called and the extra selections are cleared as well but the text is still being drawn with the error format.
Thanks for the help.
Upvotes: 2
Views: 2940
Reputation: 17388
Seems more like a Qt bug to me.
If your just looking for a fix / workaround:
Add this after tc.select(QTextCursor::Document);
:
tc.setCharFormat(textCursor().charFormat());
That should sort the issue out.
Details:
This is the function I was testing this with. I just made the function take an int num and format those char positions as an extraSelection
with custom format(Does not include fix I mentioned above).
void MainWindow::highlightErrors(int num) {
QTextCursor tc = ui_.textEdit->textCursor();
tc.select(QTextCursor::Document);
QTextCharFormat format;
QColor lineColor = QColor(Qt::red);
format.setBackground(lineColor);
// format.setFontCapitalization(QFont::AllLowercase);
QList<QTextEdit::ExtraSelection> extraSelections;
for(int i = 0; i < num; ++i) {
QTextEdit::ExtraSelection selection;
selection.format = format;
selection.format.setToolTip(QString::number(i));
selection.cursor = tc;
selection.cursor.clearSelection();
selection.cursor.setPosition(i);
selection.cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, 1);
// selection.cursor.mergeCharFormat(selection.format);
extraSelections.append(selection);
}
ui_.textEdit->setExtraSelections(extraSelections);
}
In the above code 2 lines are commented. All that happens is a red background gets applied. Now this works fine.
However if we uncomment format.setFontCapitalization(QFont::AllLowercase);
, and re-run we can see the background work as expected but the lowercase change does not get applied(make sure it's caps to begin-with) (This is what I reckon is the Qt bug since format changes only apply partially)
Now if we also uncomment selection.cursor.mergeCharFormat(selection.format);
, we can see the lowercase format is applied as well but the format removal stops working.
Hence us needing a fix to reset the CharFormat()
on entire document when errorSelection()
is changed.
Upvotes: 2