[Qt-interest] QFileInfoList and UTF-8

Nikos Chantziaras realnc at arcor.de
Thu Feb 24 13:10:57 CET 2011


On 02/24/2011 02:04 PM, Mathieu Malaterre wrote:
> Dear all,
>
>    I am trying to use QDir, but I do not understand how I should be
> dealing with UTF-8 encoded filename. Here is a simple demo that I
> tried on my linux box:
>
> $ mkdir /tmp/demo
> $ touch /tmp/demo/François.txt
> $ ./demo /tmp/demo
> /tmp/demo/François.txt
> $ echo $LANG
> en_US.utf8
>
> Here is the code for demo.cxx:
>
> #include<QDir>
> #include<QString>
> #include<iostream>  // std::cout
>
> int main(int , char *argv[])
> {
>    const char *dirname = argv[1];
>    QDir dir( QString::fromUtf8(dirname) );
>    QFileInfoList children =
> dir.entryInfoList(QDir::AllEntries|QDir::NoDotAndDotDot);
>    QFileInfo file = children.at(0);
>    std::cout<<  file.absoluteFilePath().toUtf8().constData()<<  std::endl;
>
>    return 0;
> }
>
> What am I missing from the call to entryInfoList ? I am trying to use
> a 3rd party library which expect filenames to be specified as UTF-8.

You can't assume the local filesystem uses UTF-8.  You need to use 
QString::fromLocal8Bit():

   QDir dir( QString::fromLocal8Bit(dirname) );



More information about the Qt-interest-old mailing list