How to clear selection after search and isModified flag?
Solved
General and Desktop
-
In my method:
void CodeEditor::search(const QString &searchString) { if (searchString.isEmpty()) return; bool found = false; QTextCursor highlightCursor(plainEdit->document()); QTextCursor cursor(plainEdit->document()); cursor.beginEditBlock(); QTextCharFormat plainFormat(highlightCursor.charFormat()); QTextCharFormat colorFormat = plainFormat; colorFormat.setForeground(Qt::red); colorFormat.setBackground(Qt::yellow); while (!highlightCursor.isNull() && !highlightCursor.atEnd()) { highlightCursor = plainEdit->document()->find(searchString, highlightCursor, FindFlag(0)); if (!highlightCursor.isNull()) { found = true; highlightCursor.mergeCharFormat(colorFormat); } } cursor.endEditBlock(); }
After cursor.endEditBlock() document is modified. How to avoid settings this flag?
If I remove cursor.beginEditBlock() and cursor.endEditBlock()
is a lot slower and isModified is set inhighlightCursor.mergeCharFormat(colorFormat);
- How to highlight without modification?
If I clear modification after (is was not before) it will good? - How to remove all highlighting?
Ad1:
bool modifiedBefore = plainEdit->document()->isModified(); ............ plainEdit->document()->setModified(modifiedBefore);
But point 2 I don't know how clear all yellow selection.
highlightCursor.clearSelection() is not enough. - How to highlight without modification?
-
Solution
cursor.select(QTextCursor::Document); cursor.setCharFormat(QTextCharFormat()); cursor.clearSelection();
extracted from https://stackoverflow.com/questions/46454188/pyqt-how-can-i-reset-the-charformat-of-an-entire-qtextedit