Thomas Klier
Thomas Klier

Reputation: 469

Qt: Non ASCII characters in file names are replaced by '?'

I have to work on a very reduced System. It's based on Ubuntu but not installed with the Ubuntu installer. So they are only really necessary packages and configuration. QtCreator is installed and works.

When I try to create a file with a non ASCII character, the character is replaced by ?. E.g.: TestÄ.txt will be named Test?.txt. But this only happens, when I use Qt functions. 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 the file Test?.txt. The second block works as expected. The content of the files is written correct.

Upvotes: 3

Views: 1464

Answers (2)

Thomas Klier
Thomas Klier

Reputation: 469

The systems locale was not set. I didn't realize it because someone added the configuration in the .bashrc so the locale was set in the terminal.

To fix it, I created the file /etc/default/locale with content:

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

Upvotes: 2

Benjamin T
Benjamin T

Reputation: 8311

It might be because by your compiler that does not know how to handle Ä in the source file.

You could try "Test\xC3\x84.txt" (or u8"TestÄ.txt" if you have C++17).

Also note that the QFile constructor will convert your char array to a QString. If you want to check how this goes you could add:

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

and check that you have 0xC384 for the Ä.

Upvotes: 0

Related Questions