[PyKDE] Problems with QTextView

Boudewijn Rempt boud at rempt.xs4all.nl
Wed Feb 28 19:59:59 GMT 2001

On Tuesday 27 February 2001 23:09, you wrote:

> Anyway, I'm having difficulty figuring out how to build a QTextView
> widget to my liking. I want to read the contents of a file and display
> them in a QTextView widget exactly as they appear in the file.  The
> problem is that any lines that are only carriage returns are being
> ingnored, and any leading whitespace on a line is being ignored as well.

OK, this is easy to solve: the QTextView thinks you're feeding it
rich, formatted text. Adding a 


after you've filled the view will do the trick. By the way, there are
a few points where you might have more fun if you did things more simply:

> class QtHelpBrowser(QMainWindow):
>     """
>     Class that defines a pop-up help message box.
>     """
>     def __init__(self, file):
> 	QMainWindow.__init__(self)
>         font = QFont("Courier", 12, QFont.Bold)
>         self.setFont(font)
>         lines_in_file = Utils.readlines_open(file)
>         tv = QTextView(self)
>         self.resize(700,700)
>         for i in range( len( lines_in_file ) ):
>             tv.append( QString(lines_in_file[i]) )

You can loop through the list of lines without further ado like this,
and you don't need to convert each line to a QString yourself, so you can
replace your loop with:
         for line in lines_in_file:

>         tv.resize( self.width(), self.height() )

Manually resizing widgets is in most cases a bad idea: you might want
to make tv the central widget of the window, leaving all resizing to
its built-in layout manager. Replace the preceding line with:

And of course, add:


for the desired effect. However, if you want to stun and wow your customers,
try stuffing neatly made-up html in the textview widget, instead of plain 


Boudewijn Rempt | http://www.valdyas.org

More information about the PyQt mailing list