TextEdit programmatic way to setText without trigg

2020-04-12 07:42发布

问题:

I am using pyQt to display data in a textEdit then have the connected textChanged method to send the text to a server application. I need the same behavior exhibited from the QLineEdit.textEdited as textEdited in QLineEdit does not get triggered on setText.

Is there any solutions for this? Possibly a way to detect if the change was programmatic? Thanks in advance.

回答1:

You can block the emission of the textChanged signal using blockSignals() method:

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_())