如何在pyqt5 python中突出显示qtextedit中文本中的单词?

5us2dqdw  于 2021-09-08  发布在  Java
关注(0)|答案(2)|浏览(459)

我正在用pyqt5 python创建一个文本编辑器。我想像其他任何文本/代码编辑器一样添加查找功能。在查找行编辑中输入一个或多个单词后;该单词将在文本编辑中突出显示!如何使用pyqt5 python[而不是pyqt4或任何其他编程语言]实现我的代码:

  1. class Ui_nms_pad(QMainWindow):
  2. def __init__(self):
  3. super(Ui_nms_pad, self).__init__()
  4. uic.loadUi('Nms_pad.ui', self)
  5. self.Find_Button.clicked.connect(self.Find_word())
  6. def Find_word(self):
  7. words = self.Find_lineEdit.text
  8. {highlight words in text edit}
a64a0gku

a64a0gku1#

我希望下面的代码能为您服务。

  1. def Find_word(self):
  2. self.findDialog = QtWidgets.QDialog(self)
  3. label = QtWidgets.QLabel("Find Word:")
  4. self.lineEdit = QtWidgets.QLineEdit()
  5. self.lineEdit.setText(self.lastSearchText)
  6. label.setBuddy(self.lineEdit)
  7. self.findButton = QtWidgets.QPushButton("Find Next")
  8. self.findButton.setDefault(True)
  9. self.findButton.clicked.connect(self.searchText)
  10. buttonBox = QtWidgets.QDialogButtonBox(QtCore.Qt.Vertical)
  11. buttonBox.addButton(self.findButton, QtWidgets.QDialogButtonBox.ActionRole)
  12. topLeftLayout = QtWidgets.QHBoxLayout()
  13. topLeftLayout.addWidget(label)
  14. topLeftLayout.addWidget(self.lineEdit)
  15. leftLayout = QtWidgets.QVBoxLayout()
  16. leftLayout.addLayout(topLeftLayout)
  17. mainLayout = QtWidgets.QGridLayout()
  18. mainLayout.setSizeConstraint(QtWidgets.QLayout.SetFixedSize)
  19. mainLayout.addLayout(leftLayout, 0, 0)
  20. mainLayout.addWidget(buttonBox, 0, 1)
  21. mainLayout.setRowStretch(2, 1)
  22. self.findDialog.setLayout(mainLayout)
  23. self.findDialog.setWindowTitle("Find")
  24. self.findDialog.show()
  25. def searchText(self):
  26. cursor = self.text.textCursor()
  27. findIndex = cursor.anchor()
  28. text = self.lineEdit.text()
  29. content = self.text.toPlainText()
  30. length = len(text)
  31. self.lastSearchText = text
  32. index = content.find(text, findIndex)
  33. if -1 == index:
  34. errorDialog = QtWidgets.QMessageBox(self)
  35. errorDialog.addButton("Cancel", QtWidgets.QMessageBox.ActionRole)
  36. errorDialog.setWindowTitle("Find")
  37. errorDialog.setText("Not Found\"%s\"." % text)
  38. errorDialog.setIcon(QtWidgets.QMessageBox.Critical)
  39. errorDialog.exec_()
  40. else:
  41. start = index
  42. cursor = self.text.textCursor()
  43. cursor.clearSelection()
  44. cursor.movePosition(QtGui.QTextCursor.Start, QtGui.QTextCursor.MoveAnchor)
  45. cursor.movePosition(QtGui.QTextCursor.Right, QtGui.QTextCursor.MoveAnchor, start + length)
  46. cursor.movePosition(QtGui.QTextCursor.Left, QtGui.QTextCursor.KeepAnchor, length)
  47. cursor.selectedText()
  48. self.text.setTextCursor(cursor)
展开查看全部
a0zr77ik

a0zr77ik2#

qtextedit特征 find() 它会自动突出显示文本匹配的第一次出现,并在找到文本时返回布尔值。
没有任何参数,除了搜索字符串外,搜索从当前文本光标位置开始,一直到文档末尾。在下面的示例中,如果没有找到匹配项,则通过将光标移动到文档的开头来“ Package ”搜索;这显然仅用于演示目的(如果这是首选搜索模式,您可以在开始搜索之前首先移动光标)。

  1. def find_word(self):
  2. words = self.find_lineEdit.text()
  3. if not self.textEdit.find(words):
  4. # no match found, move the cursor to the beginning of the
  5. # document and start the search once again
  6. cursor = self.textEdit.textCursor()
  7. cursor.setPosition(0)
  8. self.textEdit.setTextCursor(cursor)
  9. self.textEdit.find(words)

相关问题