[PyQt] Howto use the Qt documentation successfully - Was: Re: Access to lines of text on textEdit.

Hans-Peter Jansen hpj at urpla.net
Tue Sep 14 10:08:23 BST 2010


On Tuesday 14 September 2010, 09:48:47 Algis Kabaila wrote:
> > On Tuesday 14 September 2010, 01:54:01 Algis Kabaila wrote:
> > > Is it possible to access lines of text in a textEdit?  If so how can
> > > I find information about it?
>
> On Tuesday 14 September 2010 10:09:10 Hans-Peter Jansen wrote:
> > Depending on document type, try this:
> >
> > 	document().findBlockByLineNumber(lineNumber).text()
> >
> > Pete
>
> On Tuesday 14 September 2010 12:24:52 Henning Schröder wrote:
> > If you enter findBlockByLineNumber in Qt Assistant you will see that
> > this method  belongs to a QTextDocument object and returns a
> > QTextBlock object QTextEdit has a method called "document()" which
> > returns a QTextDocument.
> >
> > Henning
>
> In summary, the i-th line is returned by the following
>
> line = self.textEdit.document().findBlockByLineNumber(i).text(),
>
> which at least in part is identical to what Hans-Peter told me to do.
> Hennings advice to look up Qt Assistant was an invaluable help.  Thank
> you both.

Al, I cannot imagine how to work with PyQt successfully _without_ using 
assistant. Of course, Qt's class hierarchy is quite senseful most of the 
time in the first place, but due to the sheer volume of it, nobody is able 
to memorize this all. 

Just a few words on methology: 

Searching for some functionality of QTextEdit, e.g. how to get at a specific 
line via line number: 
 * Look up QTextEdit in assistant
   We read: The QTextEdit class provides a widget that is used to edit and
   display both plain and rich text. 
 * Click on more...
   We read: QTextEdit works on paragraphs and characters. A paragraph is a
   formatted string which is word-wrapped to fit into the width of the
   widget. By default when reading plain text, one newline signifies a
   paragraph.
   Sounds like we're looking for paragraphs in plain text mode
 * Check class methods, that do what we want: 
   Nothing obvious stands out
 * Check base classes:
   QTextEdit inherits from QAbstractScrollArea only, that won't help us
   much here
 * Check methods again: 
   Nothing obvious with paragraphs, but QTextDocument * document() might be
   interesting
 * Click on document() method:
   We read: Returns a pointer to the underlying document.
 * Check it out: click on QTextDocument
   We read: The QTextDocument class holds formatted text that can be viewed
   and edited using a QTextEdit
   We're getting nearer, but still no ball: check out class methods
 * It has a method: QTextBlock findBlockByLineNumber ( int lineNumber ) 
   Sounds like the best fit: click on method
 * We read: Returns the text block that contains the specified lineNumber.
   What the hell is a QTextBlock? Click:
   It encapsulates text fragments, and provides access to them
 * Check methods: QString text() sounds, like what we are looking for
   We read: Returns the block's contents as plain text.

Target reached. 

Note, how this transforms to a single line of code. Isn't it impressive, how 
much power is at our finger tips and how much joy it can be to ignore all 
this superfluous C++ decoration, that would involve much more work to get 
right ;-)

Pete


More information about the PyQt mailing list