[PyQt] Log viewer Re: [PyQt] Log viewer

Mark Summerfield mark at qtrac.eu
Fri Jan 16 16:00:45 GMT 2009


On 2009-01-16, Frédéric wrote:
> Le 16/1/2009, "Mark Summerfield" <mark at qtrac.eu> a écrit:
> >QPlainTextEdit is optimized for use as a log display and has
> >appendHtml() and appendPlainText(). For Qt 4.3 or earlier use QTextEdit
> >or QTextBrowser instead.
>
> Thanks, Mark. So, you suggest I subclass QPlainTextEdit to add the
> write() and flush() method (the logging handler mandatory interface),
> and give that widget to the logger? Then, it won't have any parent, as
> the dialog where I show the log is created on user request (and
> destroyed as soon as the user close it)... Will it be possible to
> reparent it on the fly in this dialog?

I thought you meant logging in the GUI rather than the Python standard
library's logging module. I would write a pure data class and only use
QPlainTextEdit to display the data when neeeded.

> Is there a way to have something more model/view oriented? Some sort of
> non-graphical object, the buffer (model), I can attached later to the
> QPlainTextEdit (view)?

You can kind of do it using a QTextDocument, but unless the demands are
huge using a data class and feeding a QPlainTextEdit should work okay.
(And if not you could always create a list model and create a use a
QListView.)

> I would also like to be able to change the attributes (color, font...) of
> each log entry, according to its level. This can be easily done in html,
> but then, it will be harder, later, to get back the raw text (the user
> can save the log from the log dialog). Is there any solution, here?

Well if you held the data in a separate class (or a custom model) you
could keep it as plain text but provide methods that output html for
clients that want it.

If you have 100K + log lines then a list model + QListView (+ custom
delegate if you want fine control over rendering); but anything less and
QPlainTextEdit + a simple data class should be fine. (I'm guessing about
the 100K of course but you get the point.)

-- 
Mark Summerfield, Qtrac Ltd, www.qtrac.eu
    C++, Python, Qt, PyQt - training and consultancy
        "Programming in Python 3" - ISBN 0137129297




More information about the PyQt mailing list