7

I'm trying to paste emoji's in the QT QTextEdit box but it's not getting recognized and it's showing as ??? or [][]

I'm not talking about smiley, I'm talking about emoji.

How can I go about making sure that QT's QTextEdit accepts emoji and displays them correctly?

Thank you.

Got it based on the helpful answer below:

SOLUTION:

    QFontDatabase fontDB;
    fontDB.addApplicationFont(":/Resources/fonts/seguisym.ttf");

    QApplication::setFont(QFont(QStringLiteral("Segoe UI Symbol")));
John
  • 1,699
  • 5
  • 20
  • 29
  • possible duplicate of [QWidget for showing text with small pictures (icons/emoticons)](http://stackoverflow.com/questions/23463457/qwidget-for-showing-text-with-small-pictures-icons-emoticons) – TheDarkKnight Sep 08 '14 at 15:42
  • Overriding this seems a way to go: http://qt-project.org/doc/qt-5/qtextedit.html#insertFromMimeData – hyde Sep 08 '14 at 17:20
  • @Merlin069 I don't think that is a valid duplicate, pasting probably has its own issues, out of scope of that Q. – hyde Sep 08 '14 at 17:23

2 Answers2

5

A considerable number of emoji characters are in the Unicode Standard. If you're, for example, developing with Qt 5.3 in Mac OS X 10.9, pasting emoji characters in text edits should work as it does when pasting any other character.

The reason why your application is showing ?'s and/or []'s is because the current font (perhaps the default system font) doesn't provide representations for emoji "characters". You can find a proper font out there in the web. Check this for reference.

Then you can add the font to your Qt application

QFontDatabase fontDB;
fontDB.addApplicationFont(":/A Font Supporting Emoji.ttf");

and set it as the font for your application or only your QTextEdit if you prefer

setFont(QFont(QStringLiteral("A Font Supporting Emoji")));

With this your app should be able to display emoji.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
mhcuervo
  • 2,610
  • 22
  • 33
0

The Qt documentation of QTextEdit::paste() says: To change the behavior of this function, i.e. to modify what QTextEdit can paste and how it is being pasted, reimplement the virtual canInsertFromMimeData() and insertFromMimeData() functions.

There you should be able to convert the pasted data e.g. to an HTML img element pointing to a file that is embedded into the application by ressource compiler (or to image file on disk).

Silicomancer
  • 8,604
  • 10
  • 63
  • 130