My question relates to the accepted answer to the question How to capture Python interpreter output and show in a text widget? which shows how to redirect standard output to QTextEdit.
The author, Ferdinand Beyer, defines the EmittingStream class as such:
from PyQt4 import QtCore
class EmittingStream(QtCore.QObject):
textWritten = QtCore.pyqtSignal(str)
def write(self, text):
self.textWritten.emit(str(text))
It uses the class as follows:
def __init__(self, parent=None, **kwargs):
sys.stdout = EmittingStream(textWritten=self.normalOutputWritten)
def __del__(self):
sys.stdout = sys.__stdout__
def normalOutputWritten(self, text):
"""Append text to the QTextEdit."""
cursor = self.textEdit.textCursor()
cursor.movePosition(QtGui.QTextCursor.End)
cursor.insertText(text)
self.textEdit.setTextCursor(cursor)
self.textEdit.ensureCursorVisible()
I do not understand the line that creates an instance of the EmittingStream class. The keyword argument textWritten = self.normalOutputWritten seems to connect the textWritten-signal to the normalOutputWritten slot, but I don't understand why this works.
source
share