【问题标题】:How to highlight a word from the text in qtextedit in pyqt5 python?如何在pyqt5 python的qtextedit中突出显示文本中的一个单词?
【发布时间】:2021-07-09 09:38:00
【问题描述】:

我正在 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}

【问题讨论】:

    标签: python pyqt5 qtextedit qtextdocument


    【解决方案1】:

    我希望下面的代码能达到你的目的。

        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)
    

    【讨论】:

    • 'cursor = self.text.textCursor()' 这行显示错误是什么意思
    • @NabilMustofa 请不要只是复制和粘贴答案中提供的代码:了解它的作用,如果出现错误,请尝试查找原因;那么,在不指定 what 错误的情况下编写“它显示错误”并不是很有用。在任何情况下,回答者都为您的文本编辑使用了任意名称,因为您没有提供任何名称,因此只需在 UI 中将 self.text 替换为您的 QTextEdit 对象的名称。另外,请使用以小写字母开头的变量和函数名称(更多信息请阅读官方Style Guide for Python Code)。
    • 但它没有突出显示这个词!
    【解决方案2】:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-14
      相关资源
      最近更新 更多