[PyQt] Pimping QTextEdit

Aaron Digulla digulla at hepe.com
Fri Jan 18 20:19:57 GMT 2008


I'm trying to create a clone of yWriter4 (a software that aims to help
authors of stories -- sci-fi / fantasy / whatever) with Qt and I've got
pretty far but now, I'm a bit stuck and I'd like some feedback on a few

The original application uses a RTF widget where you can mark text as
bold, italic, etc. When I started writing, I started with a more MFC or
XML approach: I'm adding markup to the text to express what it means and
let the converters decide how to display it. So for example

   <<Now wait a minute!>> he said. <<Y Stop that!>>

is something someone says. I'm using "<<" because they are easy to type
with almost any keyboard, even on a Palm. The "Y" flags yelling. "<<T"
is thinking, etc.

This works pretty well. I've created an edit mode for jEdit and was
happy ever after ... ahem ;)

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

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

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

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.

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


Aaron "Optimizer" Digulla a.k.a. Philmann Dark
"It's not the universe that's limited, it's our imagination.
Follow me and I'll show you something beyond the limits."
http://darkviews.blogspot.com/          http://www.pdark.de/

More information about the PyQt mailing list