简体   繁体   中英

How to highlight a word from the text in qtextedit in pyqt5 python?

I'm creating a text editor in pyqt5 python. and I want to add the find feature like any other text/code editor. After entering a word or words in a find line edit; that word will be highlighted in TextEdit! how can do it in pyqt5 python [not in pyqt4 or any other programming language] my code:

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}

I hope the code below will serve your purpose.

    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)

QTextEdit features find() which automatically highlights the first occurrance of the text match and returns a bool if the text has been found.
Without any argument, besides the search string, the search is done starting from the current text cursor position up to the end of the document. In the following example, the search is "wrapped" by moving the cursor to the beginning of the document in case no match has been found; this is obviously only for demonstration purposes (if this is the preferred search mode, you can just move the cursor in the first place, before starting the search).

    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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM