[PyQt] redirect stdout to QTextEdit widget

Brian Kelley kelley at eyesopen.com
Mon Feb 16 19:06:53 GMT 2009


This should do what you want.

class OutLog:
    def __init__(self, edit, out=None, color=None):
        """(edit, out=None, color=None) -> can write stdout, stderr to a
        QTextEdit.
        edit = QTextEdit
        out = alternate stream ( can be the original sys.stdout )
        color = alternate color (i.e. color stderr a different color)
        """
        self.edit = edit
        self.out = None
        self.color = color

    def write(self, m):
        if self.color:
            tc = self.edit.textColor()
            self.edit.setTextColor(self.color)

        self.edit.moveCursor(QtGui.QTextCursor.End)
        self.edit.insertPlainText( m )

        if self.color:
            self.edit.setTextColor(tc)

        if self.out:
            self.out.write(m)

Example usage:
import sys
sys.stdout = OutLog( edit, sys.stdout)
sys.stderr = OutLog( edit, sys.stderr, QtGui.QColor(255,0,0) )



On 2/16/09 1:59 PM, "NARCISO, Rui" <RUI.NARCISO at airbus.com> wrote:



Hi all,

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..

Changing the output of the ocde I'm running directly to the QTextEdit is not an option.

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.

Would anyone have a webpage or some code I could use?

Thanks in advance
Rui

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.
If you are not the intended recipient, please notify Airbus immediately and delete this e-mail.
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.
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.


_______________________________________________
PyQt mailing list    PyQt at riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20090216/3c6761f8/attachment.html


More information about the PyQt mailing list