Qt: non-ASCII characters in filenames are replaced with '?'

I need to work with a very limited System. It is based on Ubuntu, but not installed with the Ubuntu installer. Thus, they are the really necessary packages and configuration. QtCreator is installed and working.

When I try to create a file with a non-ASCII character, the character is replaced with ?

. For example: TestÄ.txt

will be called Test?.txt

. But this only happens when I use Qt functions. The C ++ standard library works.

Example:

#include <QDebug>
#include <QFile>
#include <fstream>

int main(int, char *[])
{
    const char* fileName = "TestÄ.txt";
    qDebug() << fileName;

    {
        QFile f(fileName);
        f.open(QIODevice::WriteOnly);
        f.write("QFile Äößń\n");
    }

    {
        std::ofstream f;
        f.open(fileName, std::fstream::app);
        f << "std::ofstream Äößń\n";
    }

    return 0;
}

      

There should be one file TestÄ.txt

with two lines. But the first block creates a file Test?.txt

. The second block works as expected. The content of the files is written correctly.

+3


source to share


2 answers


The system language is not set. I didn't get it because someone added the config to .bashrc

, so the locale was set in the terminal.

To fix this, I created a /etc/default/locale

content file :



LC_ALL="en_US.UTF-8"
LANG="en_US.UTF-8"

      

+1


source


It might be because your compiler doesn't know how to handle it Ä

in the source file.

You can try "Test\xC3\x84.txt"

(or u8"TestÄ.txt"

if you have C ++ 17).

Also note that the constructor will QFile

convert your char array to QString. If you want to check how this happens, you can add:



qDebug() << QString(fileName).toUtf8().toHex(); // Should print "54657374c3842e747874"

      

and make sure what you have 0xC384

for Ä

.

0


source







All Articles