def searchtext(self): #QTextDocument.FindFlags() cursorForward #QTextDocument.FindBackward() flag = QTextDocument.FindFlags() #反向搜索 searchtextexpress= self.lineEdit_searchexpression.text() if searchtextexpress is not "": print(searchtextexpress) print(self.textBrowser_testcommand.cursor()) self.textBrowser_testcommand.find(searchtextexpress,QTextDocument.FindFlag()) print(self.textBrowser_testcommand.cursor())
def __init__(self, search_and_replace, document, path_res): """ Sets up the search bar widget :param document: reference to the document """ super().__init__() logging.debug("Created Search Widget") self.search_and_replace = search_and_replace self.document = document self.path_res = path_res self.search = "" self.current = 0 self.total = 0 self.flags = QTextDocument.FindFlag(0) self.txt_no_results = "0 results" self.last_pos = 0 self.initUI() self.hide()
def onChanged(self, search): """ this handles any change the user makes to the search bar """ self.search = search # update the number of occurrences of the search if self.case_sensitive.isChecked(): list_search = self.document.toPlainText() if self.whole_word.isChecked(): list_search = list_search.split() self.total = list_search.count(search) else: list_search = self.document.toPlainText().lower() if self.whole_word.isChecked(): list_search = list_search.split() self.total = list_search.count(search.lower()) if self.total == 0 or len(self.search) == 0: self.current = 0 self.occurances.setText(self.txt_no_results) else: self.current = 1 self.occurances.setText(str(self.current) + '/' + str(self.total)) # set the cursor to the beginning of the document cursor = self.document.textCursor() cursor.setPosition(0) self.document.setTextCursor(cursor) # set up the default search flags self.flags = QTextDocument.FindFlag(0) # if search is case sensitive if self.case_sensitive.isChecked(): self.flags = self.flags | QTextDocument.FindCaseSensitively # if search is whole word sensitive if self.whole_word.isChecked(): self.flags = self.flags | QTextDocument.FindWholeWords # if the user IS searching for regex if self.regex_search.isChecked(): self.search = QRegExp(self.search) self.document.find(self.search, self.flags) self.last_pos = self.document.textCursor().position()
else: # Find forward offset = max([cursor.selectionEnd(), cursor.selectionStart()]) match = regobj.search(text, offset) if match: pos1, pos2 = match.span() fcursor = self.textCursor() fcursor.setPosition(pos1) fcursor.setPosition(pos2, QTextCursor.KeepAnchor) return fcursor def find_text(self, text, changed=True, forward=True, case=False, words=False, regexp=False): """Find text""" cursor = self.textCursor() findflag = QTextDocument.FindFlag() if not forward: findflag = findflag | QTextDocument.FindBackward if case: findflag = findflag | QTextDocument.FindCaseSensitively moves = [QTextCursor.NoMove] if forward: moves += [QTextCursor.NextWord, QTextCursor.Start] if changed: if to_text_string(cursor.selectedText()): new_position = min([cursor.selectionStart(), cursor.selectionEnd()]) cursor.setPosition(new_position) else: cursor.movePosition(QTextCursor.PreviousWord) else: