Hi,
When I try to retrieve values of environment variables with following code:
It doesn't work if the value contains special characters like french accents.
What is the solution ?
Printable View
are your sure that your locale support this caracter (locales handling under linux can be so tricky sometimes...)?
what is printed if you simply do this :
Hi,
I tried this:
Code:
qDebug("myvariable=%s", value.constData()); cout << "myvariable=" << value.constData() << endl; qDebug() << "myvariable=" << value.constData() << endl;
The result is:
Code:
myvariable=Développement myvariable=Dveloppement myvariable=Dveloppement
So, thank you for your help! No problem with qgetenv.
Why cout and qDebug() don't print correctly the value ?
I think you would need to convert the 3 line to foo.toUtf8() and the 1st line implicitly coverts it to unicode. And cout STL doesn't handle unicode.
That could be a side effect of implicit char* to QString casts, or improper locale settings which would make QString::fromLocal8Bit() use an improper text codec.
The Qt environment manipulation functions are thread-safe, but this requires that the C library equivalent functions like getenv and putenv are not directly called.
To convert the data to a QString use QString::fromLocal8Bit().
Note: on desktop Windows, qgetenv() may produce data loss if the original string contains Unicode characters NOT representable in the ANSI encoding. Use qEnvironmentVariable() instead. On Unix systems, this function is lossless.
Link: https://doc.qt.io/qt-5/qtglobal.html#qgetenv (for more information)
Wow, 11 years... got to be a new record for thread resurrection:)