3

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.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
JerryYip
  • 95
  • 8

1 Answers1

6

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
ekhumoro
  • 115,249
  • 20
  • 229
  • 336