View Full Version : How to paint unicode surrogate pairs using QPainter?

28th December 2012, 06:45
If a unicode character with code values above 65535, it will be stored using surrogate pairs, I store it in an QString object, and draw it with QPainter, but nothing will appear, is this a bug? or QPainter’s drawText just doesn’t support characters above 65535?
I have set a correct font, the same font in other text editors / word processers can display this character.
Here is a unicode character for test: http://www.unicode.org/cgi-bin/GetUnihanData.pl?codepoint=22489

QChar data[2]={0xd849,0xdc89};
QString str(data, 2);

QRect rect(0, 0, width(), height());

QPaintDevice *device = m_backingStore->paintDevice();
QPainter painter(device);

QFont songTi(tr("simsun"), 12, 12);

painter.fillRect(0, 0, width(), height(), Qt::green);
painter.drawText(QRectF(100, 100, width(), height()), str);


29th December 2012, 22:37
How about:

QString s(QChar(140425));
painter.drawText(..., s);

31st December 2012, 14:28
How about:

QString s(QChar(140425));
painter.drawText(..., s);

Thanks, but it doesn't work, I tried.

Documents for QChar:
The QChar class provides a 16-bit Unicode character.
In Qt, Unicode characters are 16-bit entities without any markup or structure. This class represents such an entity. It is lightweight, so it can be used everywhere. Most compilers treat it like a unsigned short.

I have tried to input the same character in a QTextEdit widget, it can not display this character too.
So, I think it's a bug, and I posted a bug report (https://bugreports.qt-project.org/browse/QTBUG-28853), but no one care about my bug report, maybe they are at holiday, or maybe there's too many other more important bugs to deal.

2nd January 2013, 13:45
Maybe you should first convert your character to UTF-16 and then read it with QChar?

2nd January 2013, 14:58
Yes, and I'm sure my str encoding is UTF-16.
My bug report's status is [Open] now, I hope someone can confirm and fix it before the next release.

And, by the way, do you have some experience in dealing with text input method? For example, using Qt's public API to make a text editor control that can accept input from an input method, and can control the input method window's position.
I can get the input, but don't know how to control the input method window's position.
Here (http://v.youku.com/v_show/id_XMjU5MzM0NTE2.html)is a video about how we use input method, you can see that the input method's compositing window is moving with the caret.

Thanks, wysota.

2nd January 2013, 17:52
Yes, and I'm sure my str encoding is UTF-16.

Did you try this?

QChar c(QChar::surrogateToUcs4(0xd849,0xdc89));