Find all strings wanted and select them with QPlainTextEdit::setExtraSelections()
I'm trying to highlight al开发者_如何学Cl strings find in a QPlainTextEdit widget , but find() will only return the first result. The following code isn't working out , why ?
(textview is a class derivated from QPlainTextEdit)
And please don't ask me to use QSyntaxHighlighter to set up colors , it's different.
QList<QTextEdit::ExtraSelection> extraSelections;
textview->moveCursor(QTextCursor::Start);
while ( textview->find(key) )
{
QTextEdit::ExtraSelection extra;
extra.cursor = textview->textCursor();
extraSelections.append(extra);
}
textview->setExtraSelections(extraSelections);
It's usually good to provide a little more detail about what doesn't work :)
- What text have you tried in the
QPlainTextEdit
? - What are you using for a
key
? - Can you clarify what
find
finds when running with the text specified in the first two items above? - Do you actually end up with a list of extra selections?
- Is the lack of visible highlighting the only thing not working?
I tried your code and it seems to find all the text instances correctly. The problem seems to be that you aren't actually setting any values for the format
member of extra
. After you set extra.cursor
, try setting extra.format.fontUnderline(true);
just to see if it is having any effect.
精彩评论