我正在使用 pyqt5 python 创建一个文本编辑器。我想像任何其他文本/代码编辑器一样添加查找功能。在查找行编辑中输入一个或多个单词后;该单词将在文本编辑中突出显示!如何在 pyqt5 python 中做到这一点 [而不是在 pyqt4 或任何其他编程语言中] 我的代码:
class Ui_nms_pad(QMainWindow):
def __init__(self):
super(Ui_nms_pad, self).__init__()
uic.loadUi('Nms_pad.ui', self)
self.Find_Button.clicked.connect(self.Find_word())
def Find_word(self):
words = self.Find_lineEdit.text
{highlight words in text edit}
我希望下面的代码能够满足您的目的。
def Find_word(self):
self.findDialog = QtWidgets.QDialog(self)
label = QtWidgets.QLabel("Find Word:")
self.lineEdit = QtWidgets.QLineEdit()
self.lineEdit.setText(self.lastSearchText)
label.setBuddy(self.lineEdit)
self.findButton = QtWidgets.QPushButton("Find Next")
self.findButton.setDefault(True)
self.findButton.clicked.connect(self.searchText)
buttonBox = QtWidgets.QDialogButtonBox(QtCore.Qt.Vertical)
buttonBox.addButton(self.findButton, QtWidgets.QDialogButtonBox.ActionRole)
topLeftLayout = QtWidgets.QHBoxLayout()
topLeftLayout.addWidget(label)
topLeftLayout.addWidget(self.lineEdit)
leftLayout = QtWidgets.QVBoxLayout()
leftLayout.addLayout(topLeftLayout)
mainLayout = QtWidgets.QGridLayout()
mainLayout.setSizeConstraint(QtWidgets.QLayout.SetFixedSize)
mainLayout.addLayout(leftLayout, 0, 0)
mainLayout.addWidget(buttonBox, 0, 1)
mainLayout.setRowStretch(2, 1)
self.findDialog.setLayout(mainLayout)
self.findDialog.setWindowTitle("Find")
self.findDialog.show()
def searchText(self):
cursor = self.text.textCursor()
findIndex = cursor.anchor()
text = self.lineEdit.text()
content = self.text.toPlainText()
length = len(text)
self.lastSearchText = text
index = content.find(text, findIndex)
if -1 == index:
errorDialog = QtWidgets.QMessageBox(self)
errorDialog.addButton("Cancel", QtWidgets.QMessageBox.ActionRole)
errorDialog.setWindowTitle("Find")
errorDialog.setText("Not Found\"%s\"." % text)
errorDialog.setIcon(QtWidgets.QMessageBox.Critical)
errorDialog.exec_()
else:
start = index
cursor = self.text.textCursor()
cursor.clearSelection()
cursor.movePosition(QtGui.QTextCursor.Start, QtGui.QTextCursor.MoveAnchor)
cursor.movePosition(QtGui.QTextCursor.Right, QtGui.QTextCursor.MoveAnchor, start + length)
cursor.movePosition(QtGui.QTextCursor.Left, QtGui.QTextCursor.KeepAnchor, length)
cursor.selectedText()
self.text.setTextCursor(cursor)
find()
功能,可自动突出显示文本匹配的第一次出现,并在找到文本时返回布尔值。 def find_word(self):
words = self.find_lineEdit.text()
if not self.textEdit.find(words):
# no match found, move the cursor to the beginning of the
# document and start the search once again
cursor = self.textEdit.textCursor()
cursor.setPosition(0)
self.textEdit.setTextCursor(cursor)
self.textEdit.find(words)
from PyQt5.QtWidgets import QMainWindow, QTextEdit, QWidget, QLineEdit, QVBoxLayout, QApplication
from PyQt5.QtGui import QTextCharFormat, QBrush
from PyQt5.QtCore import Qt
import sys
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.extraSelections = []
self.lineEdit = QLineEdit(placeholderText="search")
self.textEdit = QTextEdit("""Lorem Ipsum is simply dummy text of the printing
and typesetting industry.
Lorem Ipsum has been the industry's standard
dummy text ever since the 1500s, when an
unknown printer took a galley of type and
scrambled it to make a type specimen book.
It has survived not only five centuries,
but also the leap into electronic typesetting,
remaining essentially unchanged.
It was popularised in the 1960s with the release of
Letraset sheets containing Lorem Ipsum passages,
and more recently with desktop publishing software
like Aldus PageMaker including versions of Lorem Ipsum.""")
w = QWidget()
v = QVBoxLayout()
v.addWidget(self.lineEdit)
v.addWidget(self.textEdit)
w.setLayout(v)
self.setCentralWidget(w)
self.lineEdit.returnPressed.connect(self.find_word)
def appendExtraSelection(self, tc):
ex = QTextEdit.ExtraSelection()
ex.cursor = tc
ex.format.setForeground(QBrush(Qt.blue))
ex.format.setBackground(QBrush(Qt.yellow))
self.extraSelections.append(ex)
def find_word(self):
self.extraSelections.clear()
doc = self.textEdit.document()
self.find_recursion(0)
self.textEdit.setExtraSelections(self.extraSelections)
def find_recursion(self, pos):
doc = self.textEdit.document()
tc = doc.find(self.lineEdit.text(), pos)
if not tc.isNull():
self.appendExtraSelection(tc)
self.find_recursion(tc.selectionEnd())
def main():
app = QApplication([])
w = MainWindow()
w.show()
sys.exit(app.exec())
if __name__ == "__main__":
main()
QTextDocument
的查找功能。