Exception from QTextEdit textChanged signal

Viewed 3606

I'm using Python3 and PyQt5 to create an application containing form elements such as QLineEdit and QTextEdit.

I collect data from a QLineEdit as follows (this is working properly):

    self.rolAdi = QtWidgets.QLineEdit(self.centralWidget)
    self.rolAdi.setObjectName("rolAdi")

    self.rolAdi.textChanged.connect(self.textSonDurum)

def textSonDurum(self, s):
    print("Text changed...>>> "+s)

However, the same method using a QTextEdit control doesn't work - the program stops whenever I change the content of the control:

   self.IP = QtWidgets.QTextEdit(self.centralWidget)
   self.IP.setObjectName("IP")

   self.IP.textChanged.connect(self.textSonDurum)

def textSonDurum(self, s):
    print("Text changed...>>> "+s)

I researched about this problem and I found a suggestion involving using the disconnect function, but cannot find it in PyQt5.

self.IP.textChanged.disconnect()

Output Error:

TypeError: disconnect() failed between 'textChanged' and all its connections

(The full code is very long to paste it here but let me know if you need any other part).

1 Answers

The QTextEdit textChanged signal has a different signature to the QLineEdit textChanged signal, in that it doesn't pass the text that was changed. This is because QTextEdit supports rich-text (i.e. html) as well as plain-text, so you need to explicitly request the content-type you want:

   self.IP.textChanged.connect(self.textSonDurum)

def textSonDurum(self):
    print("Text changed...>>> " + self.IP.toPlainText())
Related