Hi,
When I try to retrieve values of environment variables with following code:
Qt Code:
To copy to clipboard, switch view to plain text mode
It doesn't work if the value contains special characters like french accents.
What is the solution ?
Hi,
When I try to retrieve values of environment variables with following code:
Qt Code:
To copy to clipboard, switch view to plain text mode
It doesn't work if the value contains special characters like french accents.
What is the solution ?
If a export a variable like this:
Qt Code:
export myvariable=développementTo copy to clipboard, switch view to plain text mode
Then
returns:Qt Code:
cout << "myvariable=" << value << endl;To copy to clipboard, switch view to plain text mode
Accent is missing !
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 :
Qt Code:
qDebug("myvariable=%s", value.constData());To copy to clipboard, switch view to plain text mode
Current Qt projects : QCodeEdit, RotiDeCode
Hi,
I tried this:
Qt Code:
qDebug("myvariable=%s", value.constData()); cout << "myvariable=" << value.constData() << endl; qDebug() << "myvariable=" << value.constData() << endl;To copy to clipboard, switch view to plain text mode
The result is:
Qt Code:
myvariable=Développement myvariable=Dveloppement myvariable=DveloppementTo copy to clipboard, switch view to plain text mode
So, thank you for your help! No problem with qgetenv.
Why cout and qDebug() don't print correctly the value ?
Last edited by miraks; 21st March 2009 at 11:24.
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.
Current Qt projects : QCodeEdit, RotiDeCode
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)
Last edited by mimcimm; 23rd October 2020 at 09:13. Reason: adding link
Wow, 11 years... got to be a new record for thread resurrection
Bookmarks