Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the encoding of the text in a QTextEdit in Qt?

Tags:

encoding

qt

I am obtaining the content from a QTextEdit object by using the following code:

QString text=my_QTextEdit.toPlainText();

What is the encoding that QTextEdit uses, a what encoding is used in the QString I get back from the toPlainText() call?

Thanks.

like image 499
henryyao Avatar asked Oct 04 '22 03:10

henryyao


2 Answers

QTextEdit.toPlainText() returns a QString object, which is always a unicode character string (see documentation).

The QString class provides the functions toLatin1(), toAscii() and toUtf8(), which allow you to convert the string from unicode to an 8-bit string that you can process further. So Qt handles the encoding & decoding of the string for you.

If you want to create a QString instance from a given byte-string, you can use the functions fromAscii(), fromLatin1() or fromUtf8().

like image 125
ThePhysicist Avatar answered Oct 07 '22 20:10

ThePhysicist


All controls in Qt are enabled for 16-bit characters. That means that content of a QTextEdit is Unicode (or UTF-32/UCS-4) (see also http://developer.nokia.com/Community/Discussion/showthread.php/215203-how-to-correctly-display-Unicodes-in-QPlainTextEdit).
When getting the content of a QTextEdit control (via plainText()), you get back a QString which contains Unicode.
From there on, you can convert to other format as you like: toUTF8(), toUCS4(), ...

like image 31
Kurt Pattyn Avatar answered Oct 07 '22 20:10

Kurt Pattyn