Results 1 to 10 of 10

Thread: Unicode on (Win32) console

  1. #1
    Join Date
    May 2006
    Location
    Strasbourg / France
    Posts
    5
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Unhappy Unicode on (Win32) console

    Hi,

    I'm trying to output unicode in a (Win32) console application, without success until now.
    I read on a thread that I should use a QTextStream for this. I tried but did not get any output.

    Here my code:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication a(argc, argv);
    4.  
    5. QTextStream qStdOut(stdout, QIODevice::WriteOnly);
    6. // qStdOut.setCodec("UTF-16");
    7. QString unicodeString(QChar(0x9788));
    8. qStdOut << QString("QTextStream: ") << unicodeString << QChar('\n');
    9. std::cout << "cout: " << (char*)unicodeString.utf16() << std::endl;
    10. printf("printf: %ls\n", unicodeString.utf16());
    11.  
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    The output I get is:
    cout: êù
    printf:
    The standard cout/printf don't output unicode, and the QTextStream nothing at all... no "QTextStream: ☼"
    Is there something I missed?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Unicode on (Win32) console

    Does the Windows console support UTF-16? Try writing to a file instead of stdout and check if you get correct output.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Unicode on (Win32) console

    Quote Originally Posted by Greisby
    no "QTextStream: ☼"
    Is there something I missed?
    Add qStdOut.flush().

  4. #4
    Join Date
    May 2006
    Location
    Strasbourg / France
    Posts
    5
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Unicode on (Win32) console

    Thanks for your answers.

    1. Yes the console supports unicode. I created an unicode text file «test.txt» and displayed it (type test.txt). The unicode chars are correctly displayed.
    2. thanks, I got a bit further with the flush.

    I now have:
    Qt Code:
    1. cout: êù
    2. printf:
    To copy to clipboard, switch view to plain text mode 

    I tried uncommenting the setCodec("UTF-16") and the outputs becomes:
    Qt Code:
    1. Q T e x t S t r e a m : êù
    2. cout: êù
    3. printf:
    To copy to clipboard, switch view to plain text mode 

    Seems there is still something missing...

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Unicode on (Win32) console

    Did you try saving that character you want to display from QTextStream to a file?

    Based on the fact that you get two characters on the console instead of expected single character, maybe you should assume that in doesn't use UTF-16 but some other encoding (like UTF-8).

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Unicode on (Win32) console

    Quote Originally Posted by Greisby
    I tried uncommenting the setCodec("UTF-16") and the outputs becomes[...]
    If you won't set the encoding using setCodec(), QTextStream will use QTextCodec::codecForLocale(), which probably doesn't support all Unicode characters.

  7. #7
    Join Date
    May 2006
    Location
    Strasbourg / France
    Posts
    5
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Unicode on (Win32) console

    After googling a lot, I saw that lot of people have problems displaying unicode on the console. But I didn't found a solution.

    I read that :
    1. the console font must be set to lucida TT.
    2. the code page must be forced to 1252 (or 65001 for utf-8).

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication a(argc, argv);
    4. QString unicodeString(QChar(0x9788));
    5. QTextStream qStdOut(stdout, QIODevice::WriteOnly);
    6. DWORD dwWritten;
    7.  
    8. // 1st try: replace oem with ascii
    9. #ifdef _WIN32
    10. std::cout << "Switch input to Ascii CodePage (1252): " << (::SetConsoleCP(::GetACP())?"ok":"fail") << std::endl;
    11. std::cout << "Switch output to Ascii CodePage (1252): " << (::SetConsoleOutputCP(::GetACP())?"ok":"fail") << std::endl;
    12. std::cout << "Current input CodePage: " << (unsigned int)::GetConsoleCP() << std::endl;
    13. std::cout << "Current output CodePage: " << (unsigned int)::GetConsoleOutputCP() << std::endl;
    14. #endif
    15. qStdOut << QString("QTextStream: ") << unicodeString << QChar('\n');
    16. qStdOut.flush();
    17. qStdOut.setCodec("UTF-16");
    18. qStdOut << QString("QTextStream: ") << unicodeString << QChar('\n');
    19. qStdOut.flush();
    20. std::cout << "cout: " << (char*)unicodeString.utf16() << std::endl;
    21. std::cout << "cout: " << (char*)(unicodeString.toUtf8().constData()) << std::endl;
    22. std::wcout << L"wcout: " << (wchar_t*)unicodeString.utf16() << std::endl;
    23. std::wcout << L"wcout: " << (char*)(unicodeString.toUtf8().constData()) << std::endl;
    24. printf("printf: %ls\n", unicodeString.utf16());
    25. wprintf(L"wprintf: %ls\n", unicodeString.utf16());
    26. #ifdef _WIN32
    27. WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), L"WriteConsoleW: ", 15, &dwWritten, NULL);
    28. WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), (unicodeString + '\n').utf16(), unicodeString.length()+1, &dwWritten, NULL);
    29. #endif
    30.  
    31. // 2nd try : set CP to utf-16
    32. #ifdef _WIN32
    33. std::cout << "\nSet input CP to ucs2: " << (::SetConsoleCP(1200)?"ok":"fail") << std::endl;
    34. std::cout << "Set output CP to ucs2: " << (::SetConsoleOutputCP(1200)?"ok":"fail") << std::endl;
    35. std::cout << "Current input codepage: " << (unsigned int)::GetConsoleCP() << std::endl;
    36. std::cout << "Current output codepage: " << (unsigned int)::GetConsoleOutputCP() << std::endl;
    37. #endif
    38. qStdOut << QString("QTextStream: ") << unicodeString << QChar('\n');
    39. qStdOut.flush();
    40. std::cout << "cout: " << (char*)unicodeString.utf16() << std::endl;
    41. std::wcout << L"wcout: " << (wchar_t*)unicodeString.utf16() << std::endl;
    42. printf("printf: %ls\n", unicodeString.utf16());
    43. wprintf(L"wprintf: %ls\n", unicodeString.utf16());
    44. WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), L"WriteConsoleW: ", 15, &dwWritten, NULL);
    45. WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), (unicodeString + '\n').utf16(), unicodeString.length()+1, &dwWritten, NULL);
    46.  
    47. // 3rd try : set CP to utf-8
    48. #ifdef _WIN32
    49. std::cout << "\nSet input CP to utf-8: " << (::SetConsoleCP(65001)?"ok":"fail") << std::endl;
    50. std::cout << "Set output CP to utf-8: " << (::SetConsoleOutputCP(65001)?"ok":"fail") << std::endl;
    51. std::cout << "Current input codepage: " << (unsigned int)::GetConsoleCP() << std::endl;
    52. std::cout << "Current output codepage: " << (unsigned int)::GetConsoleOutputCP() << std::endl;
    53. #endif
    54. qStdOut.setCodec("UTF-8");
    55. qStdOut << QString("QTextStream: ") << unicodeString << QChar('\n');
    56. qStdOut.flush();
    57. std::cout << "cout: " << (char*)unicodeString.toUtf8().constData() << std::endl;
    58. std::wcout << L"wcout: " << (char*)unicodeString.toUtf8().constData() << std::endl;
    59. printf("printf: %ls\n", unicodeString.toUtf8().constData());
    60. wprintf(L"wprintf: %ls\n", unicodeString.toUtf8().constData());
    61. #ifdef _WIN32
    62. WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), L"WriteConsoleW: ", 15, &dwWritten, NULL);
    63. WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), (unicodeString + '\n').utf16(), unicodeString.length()+1, &dwWritten, NULL);
    64. #endif
    65.  
    66. return a.exec();
    67. }
    To copy to clipboard, switch view to plain text mode 

    The resulting output is awfull...
    Qt Code:
    1. Switch input to Ascii CodePage (1252): ok
    2. Switch output to Ascii CodePage (1252): ok
    3. Current input CodePage: 1252
    4. Current output CodePage: 1252
    5. Q T e x t S t r e a m : &#710;—
    6. cout: &#710;—
    7. cout: é&#382;ˆ
    8. wcout: printf:
    9. wprintf: WriteConsoleW: &#38792;
    10.  
    11. Set input CP to ucs2: fail
    12. Set output CP to ucs2: fail
    13. Current input codepage: 1252
    14. Current output codepage: 1252
    15. Q T e x t S t r e a m : &#710;—
    16. cout: &#710;—
    17. printf:
    18. wprintf: WriteConsoleW: &#38792;
    19.  
    20. Set input CP to utf-8: ok
    21. Set output CP to utf-8: ok
    22. Current input codepage: 65001
    23. Current output codepage: 65001
    24. QTextStream: &#38792;
    25. cout: printf:
    26. wprintf: WriteConsoleW: &#38792;
    To copy to clipboard, switch view to plain text mode 

    It must be possible since the type command do that.
    But I didn't found and renounce (sob).

  8. #8
    Join Date
    May 2006
    Location
    Strasbourg / France
    Posts
    5
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Unicode on (Win32) console

    Damn. The wordpad converted the symbol to a 8 bit char. That's why the «type» command worked.
    I made another file with "日本", and there it isn't displayed either.
    I must conclude the the windows console is shit and definitly cannot display unicode.

  9. #9
    Join Date
    May 2006
    Location
    Strasbourg / France
    Posts
    5
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Unicode on (Win32) console

    After a good night of sleep, I rechecked my last posts and realized that putting the console code page to utf-8 seems to work.

    Qt Code:
    1. Set input CP to utf-8: ok
    2. Set output CP to utf-8: ok
    3. Current input codepage: 65001
    4. Current output codepage: 65001
    5. QTextStream: &#38792
    6. cout: printf:
    7. wprintf: WriteConsoleW: &#38792
    To copy to clipboard, switch view to plain text mode 

    &#38792 = QChar(0x9788)

    The problem is the console font. It is not unicode, and I found no way to change it...
    A copy/paste of the console output to a Wordpad works.

  10. #10
    Join Date
    Jun 2010
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Re: Unicode on (Win32) console

    I've the same problem, I cant print my native language pt-br in windows console because I can't print these characters: á é Ã* ó ú ç ...

Similar Threads

  1. QProcess and unicode problem
    By ramazangirgin in forum Qt Programming
    Replies: 0
    Last Post: 6th June 2008, 12:22

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.