Page 2 of 2 FirstFirst 12
Results 21 to 29 of 29

Thread: How to write a Russian text in console?

  1. #21
    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: How to write a Russian text in console?

    Back to first principles. What does this output:
    Qt Code:
    1. #include <QtCore>
    2. int main(int argc, char **argv)
    3. {
    4. QCoreApplication app(argc, argv);
    5. QString test1 = QString::fromUtf8("\u0623\u062F\u062E\u0644 \u0631\u0645\u0632");
    6. QString test2= QString::fromUtf8("\u041F\u0440\u0438\u0432\u0435\u0442 \u041C\u0438\u0440");
    7. qDebug() << test1;
    8. qDebug() << test2;
    9. return 0;
    10. }
    To copy to clipboard, switch view to plain text mode 
    For both Qt 4.8.5 and Qt 5.1.1 on Linux this puts the correct characters on the screen with a suitable font and bi-directional text rendering.

    I know the input encoding is correct so "?" output is indicative of a lacking font or terminal that cannot cope with the encoded out.

  2. The following 3 users say thank you to ChrisW67 for this useful post:

    8Observer8 (29th November 2013)

  3. #22
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to write a Russian text in console?

    Trying this :
    Qt Code:
    1. #include <QtCore>
    2. int main()
    3. {
    4. QString test1 = QString::fromUtf8("\u0623\u062F\u062E\u0644 \u0631\u0645\u0632");
    5. QString test2= QString::fromUtf8("\u041F\u0440\u0438\u0432\u0435\u0442 \u041C\u0438\u0440");
    6. qDebug() << test1;
    7. qDebug() << test2;
    8. return 0;
    9. }
    To copy to clipboard, switch view to plain text mode 
    outputs : "?" with some warnings:C4566: character represented by universal-character-name '\u0623' cannot be represented in the current code page(1252)
    So my system cant output such encoding ?

  4. The following 2 users say thank you to toufic.dbouk for this useful post:


  5. #23
    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: How to write a Russian text in console?

    Ignore the compiler warning for the time being.
    Is the output from running the program all ? Characters, or just the arabic ones?
    Is the result different if you set the console font to lucida and issue a "chcp 65001" command before running the program?

  6. The following 2 users say thank you to ChrisW67 for this useful post:


  7. #24
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to write a Russian text in console?

    Quote Originally Posted by ChrisW67
    Is the output from running the program all ? Characters, or just the arabic ones?
    Just the Arabic ones, English characters outputs normal as alphabets ( if that's what you mean ).

    Quote Originally Posted by ChrisW67
    Is the result different if you set the console font to lucida and issue a "chcp 65001" command before running the program?
    How to do that ? should i compile and run the .pro or .cpp file from cmd ? can i open a Qt's cmd where it has Qt's libraries added to the cmd's environment?
    Thanks for Your help.

  8. The following 2 users say thank you to toufic.dbouk for this useful post:


  9. #25
    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: How to write a Russian text in console?

    The first string is Arabic characters, the second is Cyrillic characters. This is what I see when I run it from a command prompt on Linux:
    Qt Code:
    1. chrisw@newton /tmp/tt $ ./tt
    2. "أدخل رمز"
    3. "Привет Мир"
    To copy to clipboard, switch view to plain text mode 

    I just tried my example with VS 2010 on Win 7 and get a similar result to you: question marks all round. There seem to be several different problems:
    • The compiler is mangling the UTF8 input by trying to interpret it in the local 8-bit encoding. The warnings are issued in the process and the UTF-8 encoded text doesn't make it into the object file intact.
    • When I find a way to avoid that first mangling (by using hex directly) the result is still mangled on output.

    I will try some more experiments over the weekend

  10. The following 2 users say thank you to ChrisW67 for this useful post:

    8Observer8 (29th November 2013)

  11. #26
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Re: How to write a Russian text in console?

    ChrisW67, I want to understand that, but I don't. I read about UTF-8. How professionals solve this problem? Maybe you use "Internationalization with Qt":
    - http://qt-project.org/doc/qt-5.0/qtd...alization.html
    - http://qt-project.org/doc/qt-5.0/qtq...alization.html

    How you do it in professional applications?

  12. The following user says thank you to 8Observer8 for this useful post:


  13. #27
    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: How to write a Russian text in console?

    I will try to explain what is happening. Start with this line:
    Qt Code:
    1. QString test = QString::fromUtf8("\u041F\u0440\u0438\u0432\u0435\u0442 \u041C\u0438\u0440");
    To copy to clipboard, switch view to plain text mode 
    This is what happens on Linux with GCC.

    The compiler sees the \u041f and inserts the UTF8 encoded version of the U+041f character (П) into the string. That is two bytes 0xD0 and 0x9F. It does this for the whole string. The result is a C-style string of bytes (in hex) that is the UTF8 encoded string:
    Qt Code:
    1. D0 9F D1 80 D0 B8 D0 B2 D0 B5 D1 82 20 D0 9C D0 B8 D1 80
    To copy to clipboard, switch view to plain text mode 
    We feed that into fromUtf8() and we get a valid QString with the correct characters. When the Linux program executes, qDebug() outputs the QString correctly encoded for my UTF8 terminal and I get the expected characters on screen. The same goes for QLabel.


    On Windows with MS VC++ (2010):

    The compiler sees the \u041f and and tries to map the U+041f character (П) to the system's 8-bit Windows code page before putting it in the string. Unless your system code page is Windows-1251 there is not likely to be an equivalent of П and the compiler inserts ? as a placeholder for the character it could not convert. The compiler issues a warning:
    Qt Code:
    1. warning C4566: character represented by universal-character-name '\u041F' cannot be represented in the current code page (1252)
    To copy to clipboard, switch view to plain text mode 
    It does this for the whole string. The result is a C-style string of bytes (in hex) that is not at all what you were expecting:
    Qt Code:
    1. 3F 3F 3F 3F 3F 3F 20 3F 3F 3F
    To copy to clipboard, switch view to plain text mode 
    We feed that into fromUtf8() and we get a valid QString but not the correct characters. qDebug() and QLabel cannot give the expected output now. This compiler behaviour seems to be the same regardless of what encoding the input file is or whether it has a UTF8 byte-order-mark or not.

    If I change the line to:
    Qt Code:
    1. QString test = QString::fromUtf8("\xD0\x9F\xD1\x80\xD0\xB8\xD0\xB2\xD0\xB5\xD1\x82\x20\xD0\x9C\xD0\xB8\xD1\x80");
    To copy to clipboard, switch view to plain text mode 
    I have done the UTF8 encoding and avoid the compiler's attempt to map the characters to the Windows code page.
    If I put that string on a QLabel I see the correct characters (font permitting): the data made it in.
    The qDebug() output in the console is still wrong because the QString is being mapped (again) to the local 8-bit code page with the same "?" result.

    I get Cyrillic output in a CMD console with:
    Qt Code:
    1. #include <QApplication>
    2. #include <QLabel>
    3. #include <QDebug>
    4. #include <QTextCodec>
    5.  
    6. int main(int argc, char **argv)
    7. {
    8. QApplication app(argc, argv);
    9. QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf8"));
    10. QString test = QString::fromUtf8("\xD0\x9F\xD1\x80\xD0\xB8\xD0\xB2\xD0\xB5\xD1\x82\x20\xD0\x9C\xD0\xB8\xD1\x80");
    11. qDebug() << test.toLocal8Bit();
    12. QLabel l(test);
    13. l.show();
    14. return app.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 
    If:
    • I run the program from a Windows CMD shell, and
    • set the shell font to "Lucida Console", and
    • I execute "chcp 65001" before I run the program.




    Manually doing UTF8 encoding is not a good solution, and I do not yet have a nice solution.
    There is a hotfix for VC 2010 http://stackoverflow.com/questions/6...er-arrays-in-c but it seems that did not make it into 2012 and 2013.

  14. The following 2 users say thank you to ChrisW67 for this useful post:

    8Observer8 (30th November 2013)

  15. #28
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to write a Russian text in console?

    That was a good explanation. Thanks.
    Just a notice here, i tried printing
    Qt Code:
    1. std::cout << "أدخل رمز";
    To copy to clipboard, switch view to plain text mode 
    on VS 2012, but i got ?? as output.
    Saving the file with cp 1252 ( Western ) also results in ?? obviously.
    Saving the file with UTF-8 cp 65001 results in some weird characters.
    Saving the file with cp 1256 ( Arabic ) results in weird characters too.
    That is all under Visual Studio 2012.

  16. #29
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Thumbs up Re: How to write a Russian text in console?

    I was helped here: http://www.prog.org.ru/topic_26545_0.html

    This is the solution:

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDebug>
    3. #include <QTextCodec>
    4. #include <QTextStream>
    5. #include <iostream>
    6.  
    7. QTextStream cin(stdin);
    8. QTextStream cout(stdout);
    9.  
    10. int main(int argc, char *argv[])
    11. {
    12. QCoreApplication a(argc, argv);
    13.  
    14. QString string = "Привет, Мир!";
    15. QTextCodec *codec = QTextCodec::codecForName("CP866");
    16.  
    17. // std::cout
    18. QByteArray encodedString = codec->fromUnicode(string);
    19. std::cout << "std::cout = " << encodedString.data() << std::endl;
    20.  
    21. // TextStream
    22. cout.setCodec(codec);
    23. cout << "TextStream = " << string << "\n";
    24. cout.flush();
    25.  
    26. // qDebug
    27. QTextCodec::setCodecForLocale(codec);
    28. qDebug() << "qDebug() = " << string;
    29.  
    30. // Console r/w
    31. cout << "Enter a text: ";
    32. cout.flush();
    33. cin.setCodec(codec);
    34. QString inputStr;
    35. // Read data from a console
    36. cin >> inputStr;
    37. cout << "From a console = " << inputStr << endl;
    38. cout.flush();
    39.  
    40. return a.exec();
    41. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by 8Observer8; 17th February 2014 at 09:15.

Similar Threads

  1. Qt Linguist Can't write in russian in the translation field
    By mirluk in forum Qt Tools
    Replies: 1
    Last Post: 27th June 2012, 09:44
  2. Read/Write console app
    By InterFiction in forum Newbie
    Replies: 1
    Last Post: 15th November 2011, 14:00
  3. Replies: 7
    Last Post: 13th September 2011, 13:15
  4. How to write qt4 gui up to the existing console
    By binaural in forum Qt Programming
    Replies: 4
    Last Post: 16th June 2009, 13:44
  5. Console text color
    By skyphyr in forum Qt Programming
    Replies: 4
    Last Post: 11th December 2006, 17:19

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
  •  
Qt is a trademark of The Qt Company.