[PyQt] Pimping QTextEdit

David Boddie david at boddie.org.uk
Sun Jan 20 00:51:48 GMT 2008


On Fri, 18 Jan 2008 21:19:57 +0100, Aaron Digulla wrote:

[Plain text syntax with << and >> as delimiters.]

> Now, Qt comes with QTextEdit which is pretty powerful but also "buggy"
> or at least stubborn. For example, it uses a subset of HTML but doesn't
> allow to define styles. So I can't do "<span class='yell'>", I have to
> live with "<span style=" font-family:\'Helvetica\'; font-size: 12pt;
> font-style:italic; color:#0101c3;">".

The trick is to avoid using the HTML subset. :-/

> Worse, styles "leak" to the right, so for example when "hello" is bold
> and you start typing right after the "o", all what follows afterwards
> will be bold, too. If you don't want bold, there is no simple way to do
> it. You either have to start one further to the right, duplicate the
> character after the "o" and then delete it afterwards ... if you haven't
> forgotten until then ... or you have to type and then reset the style
> when you're done or ...

This is all part of the fun you find in lots of word processors, where it's
not clear where the style ends.

> Next, I can't really control what is going on in QTextEdit. It's one of
> the most powerful widgets with the most restrictive API. Almost
> everything is private, inaccessible stuff. Undo is built in and not
> easily integrable with app-wide undo -- I can't simply tell the
> component to use the app-wide stack, I have to track signals and create
> artifical unto commands, etc.

QTextEdit is locked down very tightly, it has to be said.

> Cursors don't always send signals when 
> they move. There is no simple API to get the current line number (which
> is != the block number with word wrap). There is no way to show line
> numbers as a column in front of the text.

I think this has been done - take a look at some of the Qt 4-based IDEs
for inspiration.

> The icing of the cake: the Qt bug tracker lists a 45 pages of bugs in
> the component, some of them fixed, some of them from years old and
> marked as "oh, yeah, well ... maybe". ;)

:-(

> So ... I'm about to give up on the widget. Has anyone managed to turn
> this into a professional quality text editing component? If not, what do
> you use? QScintilla? What about word-wrapped, proportinal fonts? I mean,
>  this is 2008 after all.

Indeed. This is one area I'd love to see some improvement in. The idea
behind widgets like these is to raise the basic level of the widgets
available so that more complex and feature rich applications can be written.

> With Qt4, we get WebKit. Does anyone have any experience with using that
> as an HTML editor?

Not yet. :-( I've previously experimented with QTextEdit to try and create
a simple style-based text editor, but I'm not happy with the way it's
implemented - having to manage cursors and track the use of styles
throughout documents led to some nasty-looking code. I'm not sure if I'd
want to go back and look at it.

For a recent Qt Quarterly article (issue 24 will be online in due course),
I played with using some low-level classes for text layout to wrap text
inside shapes. I've since tried to use this as the basis for a smarter rich
text editor, but I think it would be a lot of work to make a fully-featured
text editor out of it.

The code that accompanies the "Low-Level Text Layouts" article is already
online:

  http://doc.trolltech.com/qq/

Yes, there's a Python version in the archive.

David




More information about the PyQt mailing list