QDir和QDirIterator忽略带有非ASCII文件名的文件 [英] QDir and QDirIterator ignore files with non-ASCII filenames
问题描述
以下代码无法注意到其名称中包含非ASCII字符的任何文件(具体为西里尔字符):
The following code somehow fails to notice any files with non-ASCII characters in their names (Cyrillic characters, specifically):
for (int path = 1; path < argc; path++) {
QFileInfo fi(argv[path]);
if (fi.isDir()) {
QDir dir(argv[path], "", QDir::LocaleAware, QDir::AllEntries);
qDebug() << dir.entryList();
QDirIterator it(QString(argv[path]), QDirIterator::Subdirectories);
while (it.hasNext()) {
it.next();
qDebug() << it.fileInfo().absoluteFilePath();
/* Processing; irrelevant in the context of the question */
}
}
}
我在这里做错了什么?我应该如何处理QDir和QDirIterator使他们知道西里尔文件名?
What exactly am I doing wrong here? How should I handle QDir and QDirIterator to make them aware of Cyrillic filenames?
系统区域设置是 en_US.UTF-8
。
更新:在Windows上,一切正常。
Update: On Windows, everything works correctly.
推荐答案
从QApplication本身获取cmd行参数。
Get cmd line parameters out of QApplication itself.
所以
QApplication app(argc, argv);
QStringList args = app.arguments();
for(...)
Qt将正确处理编码。但是这只会修复cmd行上unicode的问题。不知道这是否是您的主要问题。
Qt will handle encoding properly. But that will only fix problems with unicode on cmd line. Not sure if that is your main problem though.
编辑:
fromLocal8Bit()可能不起作用,因为它不是本地编码,而是utf8。所以从utf8()将工作在linux和osx(但它不会在Windows上工作)。 On * nuxes它取决于一些环境变量(LS_LANG或某事物)。我猜Qt将所有内容都考虑在内,并将其正确转换。你可以看一下QApplication的构造函数代码,如果你想知道他们在做什么。
fromLocal8Bit() probably doesn't work because it wasn't local encoding, but utf8. So fromUtf8() would work on linux and osx (but it won't work on windows). On *nuxes it depends on some environment variables (LS_LANG or something). I guess Qt takes everything into account and converts it properly. You can look at the constructor code for QApplication if you want to know exactly what they do.
这篇关于QDir和QDirIterator忽略带有非ASCII文件名的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!