1

I would like to be able to display Unicode in QGraphicsTextItem (or a subclass of it).

The only way to set text in QGraphicsTextItem seems to be

setPlainText(text);

Trying

setPlainText(QString::fromUtf8("Caf\x00e9 Frap\x00e9"));

or

QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf8"));
setPlainText("Café Frapé");

QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf8"));
setPlainText("Caf\x00e9 Frap\x00e9");

I get:

Caf? Frap?

It seems that no matter what I do (which I am not sure is correct) I do not get the output right...

Do QGraphicsTextItem support unicode ? Is maybe the setPlainText function at fault - but then what are the alternatives ? (I looked into setDocument but it also sets plain text...)

Edit - copying the special characters inside the QGraphicsTextItem works, once on screen, but still unable to place any unicode from code.

Thalia
  • 13,637
  • 22
  • 96
  • 190
  • The question suggested as a duplicate allows display of a single character, not a QString... and when posting my question I have found it, tried it, didn't work. – Thalia May 26 '15 at 19:26

2 Answers2

0

In a class inheriting QGraphicScene, I used:

        QString text(QString::fromUtf8(xt.text));
        ...
        QGraphicsTextItem *t = addText(text = text.replace("\\n", "\n"), font);

The dot source is utf8:

digraph so {
Café -> Frapé
}

And the rendering:

enter image description here

You can find here the C++ code.

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • I am sorry but I do not see how to place a text inside a class deriving from QGraphicsTextItem... I seem able to type the special characters inside the QGraphicsTextItem once displayed but I don't know how to enter them in code. – Thalia May 20 '15 at 19:25
-1

I think you should use the

QGraphicsTextItem item.
item.setHtml( "Café Frapé" );

function instead of the mentioned. Read this QGraphicsTextItem::setHtml.

p.i.g.
  • 2,815
  • 2
  • 24
  • 41