Hey,
I have a problem with my syntax highlighting when the user selects some text. It seems that when keywords are selected, non keywords on the same line disappear and vice versa.
It has something to do with the current line marker, that is created in the overridden paint event of QTextEdit.
Below is an image with no text selected:
http://nicky.xept.nl/dennis/s1.png
Now another image with text selected:
http://nicky.xept.nl/dennis/s2.png
For a small project file containing just the issue follow this link:
http://nicky.xept.nl/dennis/highlight.zip
The code for the paint event
{
QList<QTextEdit::ExtraSelection> extras;
QList<QTextCursor*> errorCursors;
// SAVE THE SELECTION TO LATER RESTORE IT!
// int start = cursor.selectionStart();
// int end = cursor.selectionEnd();
// CLEARING THE SELECTION SOLVES THE HIGHLIGHTING PROBLEM
// BUT IS NOT GOOD
// cursor.clearSelection();
// Mark current line
highlight.cursor = this->textCursor();
highlight.
format.
setProperty(QTextFormat::FullWidthSelection,
true);
highlight.
format.
setBackground(QColor(0,
255,
0));
extras << highlight;
// Add extra selections
this->setExtraSelections(extras);
// RESTORING THE SELECTION WILL INTRODUCE THE FAULT! (SAVE FIRST ABOVE)
// cursor.setPosition(start, QTextCursor::MoveAnchor);
// cursor.setPosition(end, QTextCursor::KeepAnchor);
// this->setTextCursor(cursor);
}
void CCodeWidgetText::paintEvent(QPaintEvent *event)
{
QTextEdit::ExtraSelection highlight;
QList<QTextEdit::ExtraSelection> extras;
QList<QTextCursor*> errorCursors;
QTextCursor originalCursor = this->textCursor();
QTextCursor cursor = this->textCursor();
// SAVE THE SELECTION TO LATER RESTORE IT!
// int start = cursor.selectionStart();
// int end = cursor.selectionEnd();
// CLEARING THE SELECTION SOLVES THE HIGHLIGHTING PROBLEM
// BUT IS NOT GOOD
// cursor.clearSelection();
// Mark current line
highlight.cursor = this->textCursor();
highlight.format.setProperty(QTextFormat::FullWidthSelection, true);
highlight.format.setBackground(QColor(0,255,0));
extras << highlight;
// Add extra selections
this->setExtraSelections(extras);
QTextEdit::paintEvent(event);
// RESTORING THE SELECTION WILL INTRODUCE THE FAULT! (SAVE FIRST ABOVE)
// cursor.setPosition(start, QTextCursor::MoveAnchor);
// cursor.setPosition(end, QTextCursor::KeepAnchor);
// this->setTextCursor(cursor);
}
To copy to clipboard, switch view to plain text mode
Thanks in advance for any help on this,
Sander
Bookmarks