[PyQt] problems with QTextBrowser

Dusan Zatkovsky msk.conf at seznam.cz
Wed Apr 16 11:45:57 BST 2008

On Wed 16. April 2008 11:46:26 Aaron Digulla wrote:
> Quoting Dusan Zatkovsky <msk.conf at seznam.cz>:
> > I have tried "print repr ( html )" and it prints strange charracters (
> > <\x00h\x00t\x00m\x00l\x00>\x00<\x00h ... ). Couldn't be problem there?
> Yup. The 0-byte (\x00) is the "end of text" character. The zero bytes
> come from unicode. It seems that you store the text as "Unicode 16bit
> little endian" in the database.
> To fix this, use the codec module of python to convert the unicode to
> UTF-8. This will also make the HTML take half the amount of bytes in
> the database.
> Regards,


The text is stored to DB this way:

e = QTextEdit()
doc = self.ui.textEdit.document()
text = doc.toHtml()
foo = base64.b64encode ( text )	# this is point of failure, because text is
							# QString object ( and it is utf16 as default afaik )


I am using base64 because I am storing python dictionary to db 
as "name1=value1,name2=value2" and I must guarantee that value will not 
contains '=' and ',' charracters, because I am splitting pairs by ',' and 
name/value by '=' when reading it back from db.
Maybe it will be better to use some custom repr(), which escapes all non-ascii 
charracters, including ',' and '=' to prevent conflicts. Maybe i'll change it 

So back to code. I have fixed it by:
	foo = base64.b64encode ( text.toUtf8() )

Now it works:
	html = unicode ( utils.decB64 ( data ), 'utf8' )
	iw.ui.udescTextBrowser.setHtml ( html )

Thanks a lot!


More information about the PyQt mailing list