找到所有想要的字符串并使用 QPlainTextEdit::setExtraSelections() 选择它们
我试图突出显示 QPlainTextEdit 小部件中找到的所有字符串,但 find() 会 只返回第一个结果。下面的代码无法运行,为什么?
(textview是从QPlainTextEdit派生的类)
并且请不要要求我使用QSyntaxHighlighter来设置颜色,它是不同的。
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);
I'm trying to highlight all 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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常最好提供有关无效内容的更多详细信息:)
密钥
?find
会找到什么?我尝试了你的代码,它似乎正确找到了所有文本实例。问题似乎是您实际上没有为
extra
的format
成员设置任何值。设置extra.cursor
后,尝试设置extra.format.fontUnderline(true);
只是看看它是否有任何效果。It's usually good to provide a little more detail about what doesn't work :)
QPlainTextEdit
?key
?find
finds when running with the text specified in the first two items above?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 ofextra
. After you setextra.cursor
, try settingextra.format.fontUnderline(true);
just to see if it is having any effect.