问题
I am trying to redirect "sys.stdout" to QTextEdit
, here is my code:
class Communicate(QObject):
printText = pyqtSignal()
def write(self, text):
self.printText.emit(str(text))
class UI(QWidget):
def __init__(self, parent = None):
QWidget.__init__(self)
...
self.textedit = QTextEdit(self)
self.textedit.setGeometry(400,20,220,300)
self.c = Communicate()
self.c.printText.connect(self.textedit.insertPlainText)
sys.stdout = self.c
if __name__ == "__main__":
...
When I ran the code, I got TypeError: Communicate.printText[] signal has 0 argument(s) but 1 provided
. But when I tried to provide no argument to self.printText.emit()
, it said that self.textedit.insertPlainText
needs 1 argument. Did I miss something? Your answer will be appreciated.
回答1:
You need to specify the parameters when defining the signal. Also, you should probably provide a dummy flush
method, to avoid attribute errors:
class Communicate(QObject):
printText = pyqtSignal(str)
def write(self, text):
self.printText.emit(text)
def flush(self):
pass
来源:https://stackoverflow.com/questions/46693355/pyqt5-typeerror-signal-has-0-arguments-but-1-provided