I started writing a very basic text manipulation application in Qt, without a GUI. My text contained special characters, but somehow I was not able to print those special characters, no matter what I did. I then noticed, that after adding a QCoreApplication instance (which I had previously removed, because I thought I wouldn't need it), everything worked just as it should.

Here is the code:

#include <QCoreApplication>
#include <QString>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QString s(QString::fromUtf8("aä\xc3\xa4")); // aää

    qDebug() << s;
    qDebug() << s.toAscii();
    qDebug() << s.toLatin1();
    qDebug() << s.toUtf8();
    qDebug() << s.toLocal8Bit();
    qDebug("%s", qPrintable(s));

    qDebug("%i", s.length());
    qDebug("%i", strlen(qPrintable(s)));

    return 0;
}

Output with QCoreApplication (everything works as it should):

"aää" 
"aää" 
"aää" 
"aää" 
"aää" 
aää
3
5

Output after commenting out the line, where QCoreApplication is defined (special characters are not shown anymore):

"a" 
"a" 
"a" 
"a" 
"a" 
a
3
1

Note, that already after calling qPrintabable(s), the special characters are already removed. I tested this to be sure, that QDebug is not the problem.

I also checked if the file is really encoded in UTF-8.

Why doesn't QString treat special characters correctly, when no QCoreApplication has been instantiated?

Edit
Report