<HTML>
<HEAD>
<TITLE>Re: [PyQt] redirect stdout to QTextEdit widget</TITLE>
</HEAD>
<BODY>
<FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'>This should do what you want.<BR>
<BR>
class OutLog:<BR>
def __init__(self, edit, out=None, color=None):<BR>
"""(edit, out=None, color=None) -> can write stdout, stderr to a<BR>
QTextEdit.<BR>
edit = QTextEdit<BR>
out = alternate stream ( can be the original sys.stdout )<BR>
color = alternate color (i.e. color stderr a different color)<BR>
"""<BR>
self.edit = edit<BR>
self.out = None<BR>
self.color = color<BR>
<BR>
def write(self, m):<BR>
if self.color:<BR>
tc = self.edit.textColor()<BR>
self.edit.setTextColor(self.color)<BR>
<BR>
self.edit.moveCursor(QtGui.QTextCursor.End)<BR>
self.edit.insertPlainText( m )<BR>
<BR>
if self.color:<BR>
self.edit.setTextColor(tc)<BR>
<BR>
if self.out:<BR>
self.out.write(m)<BR>
<BR>
Example usage:<BR>
import sys<BR>
sys.stdout = OutLog( edit, sys.stdout)<BR>
sys.stderr = OutLog( edit, sys.stderr, QtGui.QColor(255,0,0) )<BR>
<BR>
<BR>
<BR>
On 2/16/09 1:59 PM, "NARCISO, Rui" <<a href="RUI.NARCISO@airbus.com">RUI.NARCISO@airbus.com</a>> wrote:<BR>
<BR>
</SPAN></FONT><BLOCKQUOTE><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'><BR>
<BR>
Hi all,<BR>
<BR>
I would to redirect the standard output to a QTextEdit but I would like it to be dynamic, ie, as soon as something is printed to the stdout the contents of QTextEdit should be updated..<BR>
<BR>
Changing the output of the ocde I'm running directly to the QTextEdit is not an option.<BR>
<BR>
I have tried to do it by reassigning the sys.stdout file descriptor but I'm encountering "memory fault" errors when trying to write to the QtextEdit.<BR>
<BR>
Would anyone have a webpage or some code I could use?<BR>
<BR>
Thanks in advance<BR>
Rui<BR>
<BR>
The information in this e-mail is confidential. The contents may not be disclosed or used by anyone other then the addressee. Access to this e-mail by anyone else is unauthorised.<BR>
If you are not the intended recipient, please notify Airbus immediately and delete this e-mail.<BR>
Airbus cannot accept any responsibility for the accuracy or completeness of this e-mail as it has been sent over public networks. If you have any concerns over the content of this message or its Accuracy or Integrity, please contact Airbus immediately.<BR>
All outgoing e-mails from Airbus are checked using regularly updated virus scanning software but you should take whatever measures you deem to be appropriate to ensure that this message and any attachments are virus free.<BR>
<BR>
<BR>
_______________________________________________<BR>
PyQt mailing list <a href="PyQt@riverbankcomputing.com">PyQt@riverbankcomputing.com</a><BR>
<a href="http://www.riverbankcomputing.com/mailman/listinfo/pyqt">http://www.riverbankcomputing.com/mailman/listinfo/pyqt</a><BR>
<BR>
</SPAN></FONT></BLOCKQUOTE>
</BODY>
</HTML>