[PyKDE] QObject.trUtf8 doesn't accept unicode strings.

Rick van Hattem Rick.van.Hattem at fawo.nl
Wed Jan 3 23:54:15 GMT 2007

On Wednesday 03 January 2007 20:12, Giovanni Bajo wrote:
> > In my case the code looks like this: self.trUtf8
> > (some_message_from_the_database)
> > These are all predefined messages so it's not a problem to
> > translate them,
> What you mean: "it's not a problem to translate them"? Do you translate
> them with Qt Linguist? How do you generate the input files for Qt Linguist?
> Otherwise, how do you translate them? Do they even need translation (and if
> not, why are you even calling trUtf8())?
Currently I don't translate them at all since it's a monolanguage program, but 
for possible future cases I'm calling trUtf8.

> > why it's a unicode string? Well, getting something from the database
> > always returns a unicode string with my current settings (and with good
> > reason, some data requires it).
> Then you need to *explicitally* encode it through mytext.encode("utf-8"),
> before passing it to trUtf8(). Of course, I still need to understand your
> full scenario (see questions above), so I can't be sure this is the correct
> solution all-around, but it surely should work as a quick patch.
I guess that is the solution I was looking for, it seems I haven't read the 
Python spec carefully enough, I was assuming that unicode() would default to 

> > Perhaps I'm doing something wrong or addressing it the wrong way but
> > I fail to comprehend why trUtf8 (which is supposed to be unicode)
> > doesn't accept unicode.
> Let's put it in this way. PyQt is a very faithful wrapper around Qt C++. In
> C++, Qt's method trUtf8() accepts a char*, which is meant to represent
> *ONLY* 8-bit strings. If your application were coded in C++, you would get
> a Unicode string from the database (it would be either a QString, or
> wchar_t* which is convertible to QString). In that case, you would face
> *exactly* the same problem, since trUtf8() would refuse to accept QString.
> So, at the *very* least, you should brought up this issue to the Qt/C++
> mailing list, not here.
> The solution in C++ would be (guess what!) to call QString::toUtf8(), which
> is Qt/C++ equivalent of of unicode.encode("utf-8").
> But again, to further help you I need a better grasp of your scenario (see
> questions above).
I know the C++ tr and trUtf8 method only support char*, however since Python 
is on a much heigher level I assume it would be natural for the PyQt 
equivalent to accept all 'normal' string types in Python. So str() should 
just work, and unicode() should also work (be it with a call to 
text.encode('utf-8') for trUtf8 and a call to text.encode('ascii') for tr.
However, I see now that I should first convert it to a QString before I give 
it to trUtf8. Imho it should do that directly but it seems I am alone 
there ;)

Thanks for the help and information.

Rick van Hattem	Rick.van.Hattem(at)Fawo.nl
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20070104/7e688f3d/attachment.bin

More information about the PyQt mailing list