Rick van Hattem | 3 Jan 2007 16:13
Picon
Favicon

QObject.trUtf8 doesn't accept unicode strings.

Hello everyone,

I was just writing some code today and after a while I came across some code 
that used unicode strings, normally not a problem but I noticed that tr and 
trUtf8 both don't accept unicode strings. Would it be possible to add this 
since it would be logical to convert a python unicode string to a QT escaped 
unicode string.

Example code:
from PyQt4 import QtCore
object = QtCore.QObject()

normal_string = str('test')
unicode_string = unicode('test')

''' this works '''
object.tr(normal_string)
object.trUtf8(normal_string)

''' this doesn't '''
object.tr(unicode_string)
object.trUtf8(unicode_string)

Naturally, the bottom two give a TypeError but I would expect it to do a cast 
to str() for tr or just accept the string for trUtf8.

--

-- 
Rick van Hattem	Rick.van.Hattem(at)Fawo.nl
(Continue reading)

Phil Thompson | 3 Jan 2007 16:30
Picon

Re: QObject.trUtf8 doesn't accept unicode strings.

On Wednesday 03 January 2007 3:13 pm, Rick van Hattem wrote:
> Hello everyone,
>
> I was just writing some code today and after a while I came across some
> code that used unicode strings, normally not a problem but I noticed that
> tr and trUtf8 both don't accept unicode strings. Would it be possible to
> add this since it would be logical to convert a python unicode string to a
> QT escaped unicode string.
>
> Example code:
> from PyQt4 import QtCore
> object = QtCore.QObject()
>
> normal_string = str('test')
> unicode_string = unicode('test')
>
> ''' this works '''
> object.tr(normal_string)
> object.trUtf8(normal_string)
>
> ''' this doesn't '''
> object.tr(unicode_string)
> object.trUtf8(unicode_string)
>
> Naturally, the bottom two give a TypeError but I would expect it to do a
> cast to str() for tr or just accept the string for trUtf8.

tr() takes a char * and not a QString, so the PyQt version takes a string 
object and not a unicode object.

(Continue reading)

Rick van Hattem | 3 Jan 2007 16:57
Picon
Favicon

Re: QObject.trUtf8 doesn't accept unicode strings.

On Wednesday 03 January 2007 16:30, Phil Thompson wrote:
> On Wednesday 03 January 2007 3:13 pm, Rick van Hattem wrote:
> > Hello everyone,
> >
> > I was just writing some code today and after a while I came across some
> > code that used unicode strings, normally not a problem but I noticed that
> > tr and trUtf8 both don't accept unicode strings. Would it be possible to
> > add this since it would be logical to convert a python unicode string to
> > a QT escaped unicode string.
> >
> > Example code:
> > from PyQt4 import QtCore
> > object = QtCore.QObject()
> >
> > normal_string = str('test')
> > unicode_string = unicode('test')
> >
> > ''' this works '''
> > object.tr(normal_string)
> > object.trUtf8(normal_string)
> >
> > ''' this doesn't '''
> > object.tr(unicode_string)
> > object.trUtf8(unicode_string)
> >
> > Naturally, the bottom two give a TypeError but I would expect it to do a
> > cast to str() for tr or just accept the string for trUtf8.
>
> tr() takes a char * and not a QString, so the PyQt version takes a string
> object and not a unicode object.
(Continue reading)

Giovanni Bajo | 3 Jan 2007 17:30
Favicon
Gravatar

Re: QObject.trUtf8 doesn't accept unicode strings.

Rick van Hattem wrote:

> I am aware of that, but I think it would be nice if trUtf8 would
> support it  yes, it would have to be converted from unicode() to
> the escaped str() version.
> It's kind of pointless to use unicode
> internally (when needed) and not being able to use it because
> the tr functions don't support it.

I disagree. You are either:

1) Storing sequence of bytes encoded in UTF-8 within an unicode instance,
you are doing something wrong: you are abusing an object which is meant to
hold something else (specifically, an unicode instance is a sequence of
unicode codepoints). By stretching this, it would be almost the same if you
asked trUtf8() to accepts list of integers in range 0-255: it's obviously
not the way an encoded string is normally represented in Python.

2) Using unicode strings in your source code and asking Qt to translate
those (in which case, it's up to you to encode them to utf-8 or to latin-1
or to whatever coding you specified in QTextCodec.setCodecForTr, before
passing them to tr()). But *why* are you using *unicode* for literal strings
that are supposed to be translated? In fact, how do you expose such strings
in your source code in a way that pylupdate is able to extract them?

Your source code should look like this:

    self.trUtf8("foo bar translate this")

Notice that the return value of tr()/trUtf8() is obviously a QString
(Continue reading)

Rick van Hattem | 3 Jan 2007 18:44
Picon
Favicon

Re: QObject.trUtf8 doesn't accept unicode strings.

On Wednesday 03 January 2007 17:30, Giovanni Bajo wrote:
> Your source code should look like this:
>
>     self.trUtf8("foo bar translate this")
>
> Notice that the return value of tr()/trUtf8() is obviously a QString
> (duck-typable and convertible to Python's unicode), so your application
> will still be fully unicode. It's only the literal in the source code
> (*immediatly* surrounded by the tr() call) which is a 8-byte str.
>
> BTW: since all my sources are UTF-8, I prefer to specify it as default
> encoding for tr (QTextCodec.setCodecForTr) so that I can later use the
> shorter tr() everywhere in the code.
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, 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).

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.

--

-- 
Rick van Hattem	Rick.van.Hattem(at)Fawo.nl
On Wednesday 03 January 2007 17:30, Giovanni Bajo wrote:
> Your source code should look like this:
(Continue reading)

Tomasz Melcer | 3 Jan 2007 19:37

Re: QObject.trUtf8 doesn't accept unicode strings.

Giovanni Bajo napisaƂ(a):
> Your source code should look like this:
> 
>     self.trUtf8("foo bar translate this")
> 
> Notice that the return value of tr()/trUtf8() is obviously a QString
> (duck-typable and convertible to Python's unicode), so your application will
> still be fully unicode. It's only the literal in the source code
> (*immediatly* surrounded by the tr() call) which is a 8-byte str.
> 
> BTW: since all my sources are UTF-8, I prefer to specify it as default
> encoding for tr (QTextCodec.setCodecForTr) so that I can later use the
> shorter tr() everywhere in the code.

Just curious: what happens if I want a string to translate which 
contains any special unicode symbols, like math operators? I couldn't 
express them as 8-byte characters...

Tomasz Melcer

Giovanni Bajo | 3 Jan 2007 20:04
Favicon
Gravatar

Re: QObject.trUtf8 doesn't accept unicode strings.

Tomasz Melcer wrote:

>> Your source code should look like this:
>>
>>     self.trUtf8("foo bar translate this")
>>
>> Notice that the return value of tr()/trUtf8() is obviously a QString
>> (duck-typable and convertible to Python's unicode), so your
>> application will still be fully unicode. It's only the literal in
>> the source code (*immediatly* surrounded by the tr() call) which is
>> a 8-byte str.
>>
>> BTW: since all my sources are UTF-8, I prefer to specify it as
>> default encoding for tr (QTextCodec.setCodecForTr) so that I can
>> later use the shorter tr() everywhere in the code.
>
> Just curious: what happens if I want a string to translate which
> contains any special unicode symbols, like math operators? I couldn't
> express them as 8-byte characters...

Uh? If you call trUtf8(), it means that you are passing in a UTF-8 encoded
string. With UTF-8, you can represent any codepoint in the Unicode standard,
so there is no problem with that.

>From a *pratical* point of view, you just want to:

- Mark your Python files as UTF-8 (using the standard encoding declaration:
http://docs.python.org/ref/encodings.html).
- Use a text editor which will actually edit contents in UTF-8 form (for
instance, a Python-aware editor that will automatically adapt to the
(Continue reading)

Giovanni Bajo | 3 Jan 2007 20:12
Favicon
Gravatar

Re: QObject.trUtf8 doesn't accept unicode strings.

> 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
(Continue reading)

Rick van Hattem | 4 Jan 2007 00:54
Picon
Favicon

Re: QObject.trUtf8 doesn't accept unicode strings.

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

> > 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
(Continue reading)

Marcos Dione | 4 Jan 2007 05:19
Picon
Favicon
Gravatar

Re: broken layout

On Fri, Dec 22, 2006 at 05:13:18PM +0100, Andreas Pakulat wrote:
> The problem is very simple: pyuic doesn't recognize KMainWindow and thus
> doesn't generate the same code as for the QMainWindow:
>         self.setCentralWidget(QWidget(self,"qt_central_widget"))
>         MainWindowLayout = QVBoxLayout(self.centralWidget(),11,6,"MainWindowLayout")
> 
> and the KMainWindow create the vbox layout with "self" as parent widget.
> 
> Not sure if this will be fixed in pyuic or not, as Phil doesn't know
> wether he'll release another version of PyQt3.
> 
> A simple replace in the generated file with the above code doesn't work
> either as then there's another problem with the tabwidget. I suggest to
> build the KMainWindow ui completely by handcrafted code.

    investigating even further, I found out that if I add those two lines, I
also have to use self.centralWidget() as the parent to all the widgets that are
direct children of the window. I'll come up with a sed or perl script and post
it to the general inspection.

--

-- 
(Not so) Random fortune:
I am strongly convinced that to be a good developer one needs to be the first
user of what he develops. Quality will suffer, otherwise.
	    -- Fabio Massimo Di Nitto, Ubuntu Server team "owner".


Gmane