Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 7

Qt-interest Archive, April 2007
Drawing utf8 characters with QPainter::drawText()


Message 1 in thread

I have utf8 strings in a char array I pass to a QString constructor. I then 
use QPainter's drawText() method to draw the text, but all non-ascii 
characters are messed up.

What am I doing wrong? :)

-- 
 [ signature omitted ] 

Message 2 in thread

On Wednesday 25 April 2007 23:26:44 Erlend Hamberg wrote:
> I have utf8 strings in a char array I pass to a QString constructor. I then
> use QPainter's drawText() method to draw the text, but all non-ascii
> characters are messed up.
>
> What am I doing wrong? :)

You may want to construct the QString using QString::fromUtf8 :)

Simon

Attachment:

Attachment: pgpLUcTRkJrLG.pgp
Description: PGP signature


Message 3 in thread

On Wednesday 25 April 2007 23:36:08 Simon Hausmann wrote:
> You may want to construct the QString using QString::fromUtf8 :)

I tried that without success, but I found out the problem was that I used %1 
and %2 in the string and fed the utf8 char arrays with arg() calls. This 
clearly didn't work. :-)

Using arg(QString::fromUtf8(myCharArray)) solved the problem.

-- 
 [ signature omitted ] 

Message 4 in thread

Hi,

>> You may want to construct the QString using QString::fromUtf8 :)
> 
> I tried that without success, but I found out the problem was that I used %1 

It does work here:
	#include <QApplication>
	#include <QLabel>
	int main(int argc, char *argv[]) {
	    QApplication app(argc, argv);
	    const char s[] = { 0xc3, 0xa9, 0xc3, 0xa8, 0xc3, 0xaa, 0x00 };
	    QLabel label(QString::fromUtf8(s));
	    label.show();
	    return app.exec();
	}
The above programs correctly displays "ÃÃÃ" in a QLabel.

> and %2 in the string and fed the utf8 char arrays with arg() calls. This 
> clearly didn't work. :-)

Indeed this was probably using this QString constructor:
http://doc.trolltech.com/4.2/qstring.html#QString-7
which converts its argument using the fromAscii() function - not what you want.

--
 [ signature omitted ] 

Message 5 in thread

Dimitri wrote:
>> and %2 in the string and fed the utf8 char arrays with arg() calls. 
>> This clearly didn't work. :-)
> 
> Indeed this was probably using this QString constructor:
> http://doc.trolltech.com/4.2/qstring.html#QString-7
> which converts its argument using the fromAscii() function - not what 
> you want.

Is there a good reason it's not using fromUtf8() instead? You could 
argue there is no good reason NOT to have it assume UTF8, since ASCII is 
a subset.

-- 
 [ signature omitted ] 

Message 6 in thread

Because the platform character set might not be UTF-8?

Never assume...

Keith
**Please do not reply to me, reply to the list.**

On 04-25-2007 5:17 PM, "Paul Miller" wrote:

> Dimitri wrote:
>>> and %2 in the string and fed the utf8 char arrays with arg() calls.
>>> This clearly didn't work. :-)
>> 
>> Indeed this was probably using this QString constructor:
>> http://doc.trolltech.com/4.2/qstring.html#QString-7
>> which converts its argument using the fromAscii() function - not what
>> you want.
> 
> Is there a good reason it's not using fromUtf8() instead? You could
> argue there is no good reason NOT to have it assume UTF8, since ASCII is
> a subset.



--
 [ signature omitted ] 

Message 7 in thread

On 25.04.07 17:22:40, Keith Esau wrote:
> Because the platform character set might not be UTF-8?

If you want to be sure to read the platform character set properly you
can't use fromAscii either (unless you use latin-1 or use
setCodecForCStrings). If you do you'll end up with garbage too, as
fromAscii expects latin1 encoded bytes in that case.

> Never assume...

Right, which is why nobody should use fromAscii unless he can be
absolutely sure he'll get real ascii or latin1 (or he set the Codec for
CString before that to a reasonable value).

Andreas

-- 
 [ signature omitted ] 

Message 8 in thread

Hi,

Since the correct codec cannot be determined by Qt, it falls back on 
fromAscii(). This behavior is documented. It's also really intended as far as 
I know.

--
 [ signature omitted ] 

Message 9 in thread

On Thursday 26 April 2007 00:06:22 Dimitri wrote:
> > I tried that without success, but I found out the problem was that I used
> > %1
>
> It does work here:
> ÂÂÂÂÂÂÂÂ#include <QApplication>
> ÂÂÂÂÂÂÂÂ#include <QLabel>
> ÂÂÂÂÂÂÂÂint main(int argc, char *argv[]) {
> ÂÂÂÂÂÂÂÂ Â ÂQApplication app(argc, argv);
> ÂÂÂÂÂÂÂÂ Â Âconst char s[] = { 0xc3, 0xa9, 0xc3, 0xa8, 0xc3, 0xaa, 0x00 };
> ÂÂÂÂÂÂÂÂ Â ÂQLabel label(QString::fromUtf8(s));
> ÂÂÂÂÂÂÂÂ Â Âlabel.show();
> ÂÂÂÂÂÂÂÂ Â Âreturn app.exec();
> ÂÂÂÂÂÂÂÂ}
> The above programs correctly displays "ÃÃÃ" in a QLabel.

The problem was that I did it like this:

#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    const char s[] = { 0xc3, 0xa9, 0xc3, 0xa8, 0xc3, 0xaa, 0x00 };
    QLabel label(QString::fromUtf8("%2").arg(s)); //              <---
    label.show();
    return app.exec();
}

What fixed it was changing the arg call to use QString::fromUtf8:

    QLabel label(QString::fromUtf8("%2").arg(QString::fromUtf8(s)));

(One could argue that it wouldn't be too far fetched to assume that the first 
method would work.)

-- 
 [ signature omitted ] 

Message 10 in thread

Hi,

> The problem was that I did it like this:
> [...]

I see. That's why one should always post a minimal compilable example in teh 
first place !

--
 [ signature omitted ]