QDir 和 QDirIterator 忽略具有非 ASCII 文件名的文件
以下代码以某种方式无法注意到名称中包含非 ASCII 字符(特别是西里尔字符)的任何文件:
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 以使它们知道西里尔文文件名?
系统区域设置为 en_US.UTF-8
。
更新:在 Windows 上,一切正常。
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 */
}
}
}
What exactly am I doing wrong here? How should I handle QDir and QDirIterator to make them aware of Cyrillic filenames?
The system locale is en_US.UTF-8
.
Update: On Windows, everything works correctly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从 QApplication 本身获取命令行参数。
所以
Qt会正确处理编码。但这只能解决 cmd 行上的 unicode 问题。但不确定这是否是您的主要问题。
编辑:
fromLocal8Bit() 可能不起作用,因为它不是本地编码,而是 utf8。所以 fromUtf8() 在 Linux 和 osx 上可以工作(但在 Windows 上不起作用)。在 *nuxes 上,它取决于一些环境变量(LS_LANG 或其他变量)。我想 Qt 会考虑所有因素并正确转换它。如果您想确切地了解它们的作用,可以查看 QApplication 的构造函数代码。
Get cmd line parameters out of QApplication itself.
So
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.
EDIT:
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.
哪一部分失败了?读取指定的初始目录 argv[path] 或迭代器?如果是前者,您应该使用
QFile::decodeName
将字节字符串转换为QString
进行文件处理。默认char*
=>QString
转换使用 Latin-1,这不是您想要的文件名。Which part is failing? Reading the initial directory specified
argv[path]
or the iterator? If it's the former, you should convert byte strings toQString
for file processing usingQFile::decodeName
. The defaultchar*
=>QString
conversion uses Latin-1, which is not what you want for file names.不要像构造 QString 时那样使用 argv[path] 。这会将字符串视为 latin1 字符串(不关心西里尔字符)。尝试
在循环顶部使用,然后在各处使用
dirName
而不是argv[path]
。Don't use
argv[path]
just like that when constructing the QStrings. This will treat the string as a latin1 string (which doesn't care about cyrillic characters). Try usingat the top of your loop and then use
dirName
everywhere instead ofargv[path]
.