Unicode characters with code values above 65535 are stored using surrogate pairs, i.e. two consecutive QChars . QString::length returns the number of QChar in this string, which may differ from the number of graphemes (real characters).
To calculate the number of graphemes, you can use the QTextBoundaryFinder class.
QString str = "𝓌"; QTextBoundaryFinder finder(QTextBoundaryFinder::Grapheme, str); int count = 0; while (finder.toNextBoundary() != -1) ++count; qDebug() << count;
Or you can convert your string to a UCS-4 / UTF-32 representation and calculate the number of 32-bit characters.
QVector<uint> ucs4 = str.toUcs4(); qDebug() << ucs4.size();
source share