簡體   English   中英

如何在pyqt5 python的qtextedit中突出顯示文本中的單詞?

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

我正在 pyqt5 python 中創建一個文本編輯器。 我想像任何其他文本/代碼編輯器一樣添加查找功能。 在查找行編輯中輸入一個或多個單詞后; 該詞將在 TextEdit 中突出顯示! 如何在 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)

QTextEdit 具有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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM