My file name is testingöäüß.txt or exampleΦ.shp , When trying to use Qt utilities such as QDir and QFile, they only show up as testing.txt and example.shp. It seems that I can’t tell what encoding those classes use. I’m trying QDirIterator and QDir function entryInfoList:
QDir someDir("/home/blah"); //contains testingöäüß.txt
QDirIterator dirIter(someDir.absolutePath(), QDir::NoDotAndDotDot | QDir ::Dirs | QDir::Files);
while(dirIter.hasNext())
{
QString fileName1 = QFile::decodeName(dirIter.next().toUtf8());
std::cout << "QDirIterator Name "<< fileName1.toStdString().c_str() << std::endl;
}
QFileInfoList fileInfoList = someDir. entryInfoList(QDir::NoDotAndDotDot | QDir::Dirs | QDir::Files);
foreach(QFileInfo fileInfo, fileInfoList)
{
QString fileName1 = QFile::decodeName(fileInfo.fileName( ).toUtf8());
std::cout << "entryInfoList Name "<< fileName1.toStdString().c_str() << std::endl;
QString fil eName2 = QFile::decodeName(fileInfo.absoluteFilePath().toUtf8());
std::cout << "entryInfoList Name2 "<< fileName2.toStdString().c_str() << std::endl;
QString fileName3 = QString::fromUtf8(dirIter.fileInfo().absoluteFilePath().toStdString().c_str());
std::cout << "entryInfoList Name3 "<}
Each of these prints are missing non-ascii characters. It seems as long as you try to catch the file name loop They will just be ascii. Anyone have any ideas on this? Or Qt can’t handle this at all? Thanks!
It turns out that QTextCodec::codecForLocale on my development virtual machine is set to UTF-8, while on the embedded box it is System. If I execute any Before the file system operation, manually change the locale to UTF-8 (by calling QTextCodec::setCodecForLocale(QTextCodec::codecForName(“UTF-8”))), everything started to work normally.
So why Does this happen in the first place? I suspect that in the process of reducing the root file system of the embedded system, I may accidentally delete some locale-related files that Qt uses to try to automatically detect the locale. When it cannot be sure that it is on UTF-8 , It fell back to System again, and it was broken for whatever reason (probably for the same reason, it could not detect UTF-8 in the first place).
I need to finally solve the problem that it cannot automatically Any problems detected, but in the short term, you only need to manually set the UTF-8 locale even if you encounter the same problem. It should be effective.
Please note that this is related to whether the console can display UTF-8 or not. Manual conversion of UTF-16 to UTF-8 is irrelevant! So Felix’s answer to this question is incorrect, at least on this question. In order to completely remove the functionality of the console from the equation, I also simply printed UTF-16 in the string The number of characters, and each non-ASCII character actually makes the returned path and the file name string from QDir::entryInfoList one less UTF-16 character. In addition, the gift of death is that the character is simply stripped, not just Is it replaced with garbage or question marks or other things.
I encountered a problem that QDir lost non-Ascii characters from my file name.
< p>My file is named testingöäüß.txt or exampleΦ.shp, when trying to use Qt utilities such as QDir and QFile, they only show up as testing.txt and example.shp. It seems that I can’t tell those classes what kind of encoding to use .I am trying QDirIterator and QDir function entryInfoList:
QDir someDir("/home/blah"); //contains testingöäüß.txt
QDirIterator dirIter(someDir.absolutePath(), QDir::NoDotAndDotDot | QDir::Dirs | QDir::Files);
while(dirIter.hasNext())
{
QString fileName1 = QFile: :decodeName(dirIter.next().toUtf8());
std::cout << "QDirIterator Name "<< fileName1.toStdString().c_str() << std::endl;
}
QFileInfoList fileInfoList = someDir.entryInfoList(QDir::NoDotAndDotDot | QDir::Dirs | QDir::Files);
foreach(QFileInfo fileInfo, fileInfoList)
{
QString fileName1 = QFile: :decodeName(fileInfo.fileName().toUtf8());
std::cout << "entryInfoList Name "<< fileName1.toStdString().c_str() << std::endl;
QString fileName2 = QFile::decodeName(fileInfo.absoluteFilePath().toUtf8());
std::cout << "entryInfoList Name2 "<< fileName2.toStdString().c_str() << std::endl;
QString fileName3 = QString::fromUtf8(dirIter.fileInfo().absoluteFilePath().toStdString().c_str());
std::cout << "entryInfoList Name3" << fileName3.toStdString().c_str() << std::endl;
}
Each of these prints is missing non-ascii characters. It seems as long as you try Catch the file names and loop them and they will just be ascii. Anyone have any ideas on this? Or Qt can’t handle this at all? Thanks!
I know this is an old problem, but I ran into the same problem. The same precise Qt code works fine on my development virtual machine, But when I transferred it to an embedded Linux system (running on x86, so literally the same executable file), my directory names just silently removed their non-ASCII characters.
It turns out that QTextCodec::codecForLocale on my development virtual machine is set to UTF-8, while on the embedded box it is System. If I manually change the locale to UTF- before performing any file system operations 8 (By calling QTextCodec::setCodecForLocale(QTextCodec::codecForName(“UTF-8”))), everything started to work normally.
So why does this happen in the first place? I suspect that in the process of reducing the root file system of the embedded system, I may accidentally delete some locale-related files that Qt uses to try to automatically detect the locale. When it cannot be sure that it is on UTF-8 , It fell back to System again, and it was broken for whatever reason (probably for the same reason, it could not detect UTF-8 in the first place).
I need to finally solve the problem that it cannot automatically Any problems detected, but in the short term, you only need to manually set the UTF-8 locale even if you encounter the same problem. It should be effective.
Please note that this is related to whether the console can display UTF-8 or not. Manual conversion of UTF-16 to UTF-8 is irrelevant! So Felix’s answer to this question is incorrect, at least on this question. In order to completely remove the functionality of the console from the equation, I also simply printed UTF-16 in the string The number of characters, and each non-ASCII character actually makes the returned path and the file name string from QDir::entryInfoList one less UTF-16 character. In addition, the gift of death is that the character is simply stripped, not just It is replaced with garbage or question marks or other things.