I want to convert QStrings into filenames. Since I'd like the filename to look clean, I want to replace all non-letters and non-numbers by an underscore. The following code should do that.
#include <iostream>
#include <QString>
QString makeFilename(const QString& title)
{
QString result;
for(QString::const_iterator itr = title.begin(); itr != title.end(); itr++)
result.push_back(itr->isLetterOrNumber()?itr->toLower():'_');
return result;
}
int main()
{
QString str = "§";
std::cout << makeFilename(str).toAscii().data() << std::endl;
}
However, on my computer, this does not work, I get as an output:
�_
Looking for an explentation, debugging tells me that QString("§").size()
= 2 > 1 = QString("a").size()
.
My questions:
- Why does QString use 2 QChars for "§"? (solved)
- Do you have a solution for
makeFilename
? Would it also work for Chinese people?
In addition to what others have said, keep in mind that a
QString
is a UTF-16 encoded string. A Unicode character that is outside of the BMP requires 2QChar
values working together, called a surrogate pair, in order to encode that character. The QString documentation says as much:You are not taking that into account when looping through the
QString
. You are looking at eachQChar
individually without checking if it belongs to a surrogate pair or not.Try this instead: