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?