Results 1 to 6 of 6

Thread: QString to TCHAR or TCHAR*

  1. #1
    Join Date
    May 2013
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default QString to TCHAR or TCHAR*

    Hi all,
    I have been working since hours to figure out a way to convert a QString value to TCHAR / TCHAR*
    have tried many forms and types..still no joy.
    please can i get a code example to convert a QString value to TCHAR / TCHAR*

    QString Value to TCHAR tValue
    and QString Value to TCHAR* rValue

    Thanks a lot.

  2. The following user says thank you to subrat17june for this useful post:


  3. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QString to TCHAR or TCHAR*

    You are asking in the wrong forum, you'll need to check with a Microsoft forum on how to create strings of their types from standard types like char* or std::string

    Cheers,
    _

  4. The following user says thank you to anda_skoa for this useful post:


  5. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QString to TCHAR or TCHAR*

    In builds where UNICODE is defined (generally the case) TCHAR is a typedef for/equivalent to WCHAR a single 16bit Unicode code point. It is clearly not possible to put a whole QString into a single Unicode character. If you have a buffer of TCHARs, that you address through a pointer, then QString::toUtf16() gives you characters you can copy into that buffer.

    In builds where UNICODE is not set TCHAR is a typedef for char. It is clearly not possible to put a whole QString into a single char. In these cases any of QString::toUtf8(), toAscii(), toLatin1(), toLocal8bit() will give you something that may make sense in a buffer of TCHARs.

  6. The following user says thank you to ChrisW67 for this useful post:


  7. #4
    Join Date
    May 2013
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QString to TCHAR or TCHAR*

    Hello ChrisW67,
    QT uses UTF-8 encoding.
    And i have googled..issue is not from QString to char*
    but
    QString to TCHAR which is a (WCHAR in unicode)

    can i get some code help??
    QString myString to TCHAR wString ??

  8. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QString to TCHAR or TCHAR*

    Did you actually try anything?
    Qt Code:
    1. const QString name("COMPUTERNAME");
    2.  
    3. // Via a std::wstring
    4. WCHAR result1[128];
    5. std::wstring s = name.toStdWString();
    6. DWORD len = GetEnvironmentVariable(s.c_str(), result1, 128);
    7. qDebug() << QString::fromWCharArray(result1);
    8.  
    9. // Via a buffer
    10. WCHAR result2[128];
    11. wchar_t buf[name.length() + 1];
    12. int l = name.toWCharArray(buf);
    13. buf[l] = 0;
    14. len = GetEnvironmentVariable(buf, result2, 128);
    15. qDebug() << QString::fromWCharArray(result2);
    16.  
    17. // Using utf16()
    18. WCHAR result3[128];
    19. len = GetEnvironmentVariable(reinterpret_cast<LPCTSTR>(name.utf16()), result3, 128);
    20. qDebug() << QString::fromWCharArray(result3);
    To copy to clipboard, switch view to plain text mode 

  9. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QString to TCHAR or TCHAR*

    Quote Originally Posted by subrat17june View Post
    QT uses UTF-8 encoding.
    No. From the Qt Documentation:
    "QString stores a string of 16-bit QChars, where each QChar corresponds one Unicode 4.0 character. "

    Cheers,
    _

Similar Threads

  1. URLDownloadToFile, TCHAR, string, wrong type.
    By darkrptyp in forum Qt Programming
    Replies: 5
    Last Post: 9th December 2011, 01:22
  2. Replies: 2
    Last Post: 11th August 2011, 15:42
  3. Replies: 2
    Last Post: 17th December 2010, 08:26
  4. Convert QString to TCHAR
    By sheeeng in forum Qt Programming
    Replies: 1
    Last Post: 8th January 2010, 07:37
  5. Retrieving QString from TCHAR
    By GTBuilder in forum Newbie
    Replies: 6
    Last Post: 28th March 2008, 06:43

Tags for this Thread

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.