[PyKDE] Inserting Rich Text in a QTextEdit

Michael Sparks zathras at thwackety.com
Mon Dec 20 04:24:01 GMT 2004


Hi,


I've been using a QTextEdit for a while as a simple rich text editor, and
recently been playing with various ideas and have hit a bit of a block.
Essentially what I want to do is this:

   editor.insert("<a href='"+str(page)+"'>"+str(page)+"</a>"  )

And have that rendered _and stored in the text_(*) as a hyperlink - ie the
above line results in (with defaults) "page" being rendered underlined in
blue.
   (*) Hence a stylesheet isn't an option here :-(

Instead however what I see in the editor is this:

  <a href='Somepage'>Somepage</a>

(Which gets saved as &lt;a href ... etc )

However, if I use this:

   editor.append("<a href='"+str(page)+"'>"+str(page)+"</a>"  )

This is correctly interpreted as being a hyperlink, and saved as a
hyperlink.

The question I have really is this - is there any way round this, short of
building up this behaviour from scratch doing something like:

(pseudocode)

   if hasSelectedText
      t = selectedText
      removeSelectedText
      p = getCursorPosition
      setCursorPosition(end of text)
      q = getCursorPosition
      append("thing I want")
      r= setCursorPosition(end of text)
      setSelection(between q and r)
      cut
      setCursorPosition(p)
      paste

?

It seems like a lot of faff ? (obviously I'd wrap that up in a function,
probably called something like insertRichText(), but even so... (I'd be
happy with a "replace selection" option instead, but I can't see that -
hopefully I'm being blind :) )

Failing that, if I really have missed something and there's a much simpler
way of doing this I'd really appreciate a pointer, slap round the head :)

Thanks for any responses,


Michael.




More information about the PyQt mailing list