Results 1 to 9 of 9

Thread: qgetenv and special chars

  1. #1
    Join Date
    Aug 2008
    Posts
    70
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default qgetenv and special chars

    Hi,

    When I try to retrieve values of environment variables with following code:
    Qt Code:
    1. QString value=QString::fromLocal8Bit( qgetenv(iAttribute.toUtf8().constData()).constData() );
    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 ?

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qgetenv and special chars

    Quote Originally Posted by miraks View Post
    It doesn't work if the value contains special characters like french accents.
    Which value ? And what exactly does not work as expected? Do you get an empty QString? an ugly QString? something else?
    Current Qt projects : QCodeEdit, RotiDeCode

  3. #3
    Join Date
    Aug 2008
    Posts
    70
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: qgetenv and special chars

    If a export a variable like this:
    Qt Code:
    1. export myvariable=développement
    To copy to clipboard, switch view to plain text mode 

    Then
    Qt Code:
    1. QString value=QString::fromLocal8Bit( qgetenv("myvariable").constData() );
    2. cout << "myvariable=" << value << endl;
    To copy to clipboard, switch view to plain text mode 
    returns:
    Qt Code:
    1. myvariable=dveloppement
    To copy to clipboard, switch view to plain text mode 

    Accent is missing !

  4. #4
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qgetenv and special chars

    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:
    1. QByteArray value = qgetenv("myvariable");
    2. qDebug("myvariable=%s", value.constData());
    To copy to clipboard, switch view to plain text mode 
    Current Qt projects : QCodeEdit, RotiDeCode

  5. #5
    Join Date
    Aug 2008
    Posts
    70
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: qgetenv and special chars

    Hi,

    I tried this:
    Qt Code:
    1. QByteArray value = qgetenv("myvariable");
    2. qDebug("myvariable=%s", value.constData());
    3. cout << "myvariable=" << value.constData() << endl;
    4. qDebug() << "myvariable=" << value.constData() << endl;
    To copy to clipboard, switch view to plain text mode 

    The result is:
    Qt Code:
    1. myvariable=Développement
    2. myvariable=Dveloppement
    3. myvariable=Dveloppement
    To 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 12:24.

  6. #6
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: qgetenv and special chars

    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.
    Qt-4.7.3 | Gentoo ~amd64 | KDE-4.7
    Aki IRC Client for KDE4 | Qt Documentation

  7. #7
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qgetenv and special chars

    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

  8. #8
    Join Date
    Oct 2020
    Posts
    1
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: qgetenv and special chars

    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 10:13. Reason: adding link

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,679
    Thanks
    13
    Thanked 1,597 Times in 1,525 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: qgetenv and special chars

    Wow, 11 years... got to be a new record for thread resurrection

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.