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

Giovanni Bajo rasky at develer.com
Wed Jan 3 19:12:21 GMT 2007


> 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())?

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

> 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).
-- 
Giovanni Bajo




More information about the PyQt mailing list