QDir and QDirIterator ignore files with names other than ASCII

The following code somehow does not notice any files with non-ASCII characters in their names (for example, Cyrillic characters):

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 do I handle QDir and QDirIterator so that they know about Cyrillic file names?

The locale of the system en_US.UTF-8.

Update: On Windows, everything is working correctly.

+3
source share
3 answers

Get cmd line parameters from QApplication itself.

So,

QApplication app(argc, argv);

QStringList args = app.arguments();

for(...)

Qt will correctly handle the encoding. But this will only fix unicode issues in the cmd line. Not sure if this is your main problem.

EDIT: fromLocal8Bit(), , , , utf8. fromUtf8() linux osx ( windows). On * nuxes (LS_LANG - ). , Qt . QApplication, , .

+2

? , argv[path] ? , QString QFile::decodeName. char* = > QString Latin-1, .

+1

argv[path] , QStrings. latin1 ( ).

const QString dirName = QString::fromLocal8Bit( argv[path] );

at the top of your loop, and then use dirNameeverywhere, not argv[path].

0
source

Source: https://habr.com/ru/post/1721369/


All Articles