Search code examples
qtqgraphicsview

How to render emoji's in with QGraphicsTextItem?


Here is the thing. I've created a little Qt chat application for my work office. It is only for lan connected clients. I implemented this by using QGraphicsTextItem for the messages inside bubbles which are a rounded rectancles.

To really complete it I need to add emojis to it. All chat-like applications have them. The thing is that I don't know how to implement them. I've read that they are part of the unicode standad. (the unicode codes for this link proves it http://unicode.org/emoji/charts/full-emoji-list.html), but I have no idea how to render it in a QGraphicsTextItem. Is it even possible?

I could do a very very manual labor of separating the user string and actually use multiple text items to render the parts fo the strings separated by emojis y and then the emoji themselves as a separate graphical entity, but I rather not do that if I can avoid it.

Any ideas?


Solution

  • There's nothing to do: the emojis are simply characters, you can copy-paste them from the "Brow" column in the page you cited, just like you would any other character e.g. from this answer (including the emoji!). A sane compiler assumes that your source is UTF-8 encoded, as do most editors, so it will be transparent. If the relevant fonts are installed, it will just work.

    // https://github.com/KubaO/stackoverflown/tree/master/questions/emoji-text-item-38000855
    #include <QtWidgets>
    
    int main(int argc, char ** argv) {
       QApplication app{argc, argv};
       QGraphicsScene scene;
       QGraphicsView view{&scene};
       QGraphicsTextItem item{QStringLiteral("I'm a happy 👧. 😀")};
       scene.addItem(&item);
       view.ensureVisible(scene.sceneRect());
       view.show();
       return app.exec();
    }