def search_highlight(self):
cursor = self.text_field.textCursor()
b_format = cursor.blockFormat()
b_format.setBackground(QBrush(QColor('white')))
cursor.setBlockFormat(b_format)
format = QTextCharFormat()
format.setBackground(QBrush(QColor('yellow')))
regex = QRegularExpression(self.search_field.text())
matches = regex.globalMatch(self.text_field.toPlainText())
_matches = []
while matches.hasNext():
_matches.append(matches.next())
self.search_matches = _matches
self.search_field_matches.setText('Matches: ' + str(len(self.search_matches)))
self.search_field_matches.show()
self.search_field_progress.setRange(0, len(self.search_matches))
if len(self.search_matches) > 100:
self.search_field_progress.show()
match_count = 1
for match in self.search_matches:
if match_count > 150:
# TODO: implement proper handling of > 1000 matches
break
self.search_field_progress.setValue(match_count)
match_count += 1
cursor.setPosition(match.capturedStart())
cursor.setPosition(match.capturedEnd(), QTextCursor.KeepAnchor)
cursor.mergeCharFormat(format)
#self.field.moveCursor(QTextCursor.Start)
#self.field.moveCursor(F)
#self.field.ensureCursorVisible()
评论列表
文章目录