Python TextEdit是否以编程方式设置文本而不触发textChanged事件?

Python TextEdit是否以编程方式设置文本而不触发textChanged事件?,python,pyqt,pyqt5,Python,Pyqt,Pyqt5,我使用pyQt在文本编辑中显示数据,然后使用connected textChanged方法将文本发送到服务器应用程序。我需要QLineEdit.textEdit中显示的相同行为,因为QLineEdit中的textEdit不会在setText上触发 有什么解决办法吗?可能是一种检测更改是否为编程更改的方法?提前感谢。您可以使用以下方法阻止textChanged信号的发射: from PyQt5 import QtCore, QtGui, QtWidgets class MainWindow(Qt

我使用pyQt在文本编辑中显示数据,然后使用connected textChanged方法将文本发送到服务器应用程序。我需要QLineEdit.textEdit中显示的相同行为,因为QLineEdit中的textEdit不会在setText上触发


有什么解决办法吗?可能是一种检测更改是否为编程更改的方法?提前感谢。

您可以使用以下方法阻止textChanged信号的发射:

from PyQt5 import QtCore, QtGui, QtWidgets

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.text_edit = QtWidgets.QTextEdit(
            textChanged=self.on_textChanged
        )
        self.setCentralWidget(self.text_edit)

        timer = QtCore.QTimer(
            self, 
            timeout=self.on_timeout
        )
        timer.start()

    @QtCore.pyqtSlot()
    def on_textChanged(self):
        print(self.text_edit.toPlainText())

    @QtCore.pyqtSlot()
    def on_timeout(self):
        self.text_edit.blockSignals(True)
        self.text_edit.setText(QtCore.QDateTime.currentDateTime().toString())
        self.text_edit.blockSignals(False)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())