Page 1 of 2 12 LastLast
Results 1 to 20 of 29

Thread: How to write a Russian text in console?

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

    Default How to write a Russian text in console?

    Hi,

    How to write a Russian text in console?

    This is my code:

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDebug>
    3. #include <QTextCodec>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication a(argc, argv);
    8.  
    9. QTextCodec *russian =QTextCodec::codecForName("CP1251");
    10. QTextCodec::setCodecForTr(russian);
    11. qDebug() << QObject::tr("Привет Мир");
    12.  
    13. return a.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    Output:
    "╨Я╤А╨╕╨■“╨╡╤В ╨Ь╨╕╤А"
    Thank you!

  2. The following 2 users say thank you to 8Observer8 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: How to write a Russian text in console?

    I think qDebug() doesn't use codecs, however you could try:
    - using toLocal8Bit() on the string returned from tr()
    - use the QTextCodec to convert the string to QByteArray
    - use a QTextStream with the codec on the stdout file handle

    Cheers,
    _

  4. The following 3 users say thank you to anda_skoa for this useful post:

    8Observer8 (29th September 2013)

  5. #3
    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?

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDebug>
    3. #include <QTextCodec>
    4. #include <QTextStream>
    5.  
    6. QTextStream cout(stdout);
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QCoreApplication a(argc, argv);
    11.  
    12. QTextCodec *russian =QTextCodec::codecForName("CP1251");
    13. QTextCodec::setCodecForTr(russian);
    14. cout << QObject::tr("Привет Мир");
    15. cout.flush();
    16.  
    17. return a.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

    Output:
    ╨Я╤А╨╕╨■╨╡╤В ╨Ь╨╕╤А

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


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

    That is essentially the same thing.

    Qt Code:
    1. cout << QObject::tr("Привет Мир");
    To copy to clipboard, switch view to plain text mode 
    Since there is no operator<<(std:stream&, const QString&) this will cause an implicit conversion from QString to something that does have such an operator.
    And that thing is const char*. Now the cast operator that creates const char* from QString uses the same encoding as QString::toLatin1(). Your Russian text is not latin1.

    Cheers,
    _

  8. The following 3 users say thank you to anda_skoa for this useful post:

    8Observer8 (29th September 2013)

  9. #5
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

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

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication a(argc, argv);
    7.  
    8. qDebug() << QString::fromUtf8("Привет Мир");
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    At least, this works on Debian. QString is Unicode internally so that no problems with Cyrillic but you will need a Unicode font. If you need to output char * or QByteArray recode to Unicode ordinals using fromUtf8() first.

  10. The following 3 users say thank you to Radek for this useful post:

    8Observer8 (29th September 2013)

  11. #6
    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?

    It doesn't work:

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication a(argc, argv);
    7.  
    8. qDebug() << QString::fromUtf8("Привет Мир");
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    Output:
    "╧ЁштхЄ ╠шЁ"
    I will read about it. Thank you.

  12. The following 2 users say thank you to 8Observer8 for this useful post:


  13. #7
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

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

    Then it is the font, it isn't Unicode or it uses some strange encoding. Note that the number of letters is now correct (it wasn't before, the output was a wrong interpreted Unicode encodings). Also, it can be a missing or incomplete support of Unicode on your machine. Check the contents of the string.

    Qt Code:
    1. QString str = QString::fromUtf8("Привет Мир");
    2. int len = str.size();
    3. int chk;
    4.  
    5. for( int i = 0; i < len; i++ )
    6. {
    7. chk = str.at(i).unicode();
    8. }
    To copy to clipboard, switch view to plain text mode 

    Run debugger and see the values in chk. They should be unicode ordinals: 0x41F 0x440 0x438 0x432 0x442 0x20 0x41C 0x438 0x440. If they aren't then the unicode support isn't good (as you type the text to the editor, a wrong encodings are generated). If they are then the output font isn't unicode.

  14. The following 3 users say thank you to Radek for this useful post:

    8Observer8 (29th September 2013)

  15. #8
    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?

    Hey ,
    i experienced something similar to this.
    first try to add your Russian text in a combo box and see it if correctly shows.
    check your system's encoding and get familiar with it.
    save your source file as UTF-8.

  16. The following 3 users say thank you to toufic.dbouk for this useful post:

    8Observer8 (29th September 2013)

  17. #9
    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?

    Yes! It works! I will be trying yet.

    72.png

    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3.  
    4. int main(int argc, char *argv[]) {
    5. QApplication app(argc, argv);
    6.  
    7. QLabel *lblText = new QLabel(QString::fromUtf8("Привет Мир!"));
    8. lblText->show();
    9.  
    10. return app.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

  18. The following 2 users say thank you to 8Observer8 for this useful post:


  19. #10
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

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

    This solves labels and other GUI strings and shows that the internal Unicode traffic works at your machine. It also shows that the "Привет Мир!" literal is processed correctly by your editor (where you write your code). But it does not solve the console output (on my Debian Wheezy the console works). Try setting another font for the console (there should be some, because labels work) or try another console (from Qt Creator: Tools -> Options -> Environment -> Terminal).

  20. The following 2 users say thank you to Radek for this useful post:


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

    Just checking: you have tried, lets say my first suggestion, right?
    I.e. using toLocal8Bit() and it did not work?

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication a(argc, argv);
    7.  
    8. qDebug() << QString::fromUtf8("Привет Мир").toLocal8Bit();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  22. The following 2 users say thank you to anda_skoa for this useful post:


  23. #12
    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?

    using toLocal8Bit() and it did not work?
    .
    Qt Code:
    1. qDebug() << QString::fromUtf8("/*some Arabic word*/").toLocal8Bit();
    To copy to clipboard, switch view to plain text mode 
    in my case ( trying to show Arabic alphabets ) it just shows * ??? * instead of the actual letters.
    Just want to point this out.

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


  25. #13
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

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

    untested you can try...

    or set
    setCodecForTr(russian); to out && in

    Qt Code:
    1. int main(int argc, char *argv[]) {
    2. QApplication app(argc, argv);
    3. QTextCodec::setCodecForTr(QTextCodec::codecForName("CP1251"));
    4.  
    5. QTextStream out(stdout);
    6. /// QTextCodec *russian = QTextCodec::codecForName("CP1251");
    7. /// or out.setCodecForTr(russian);
    8. QString str("*");
    9. out << str.fill('*', 80) << "\n";
    10. out.flush();
    11.  
    12. out << "Please enter word to search russian keyboard:\n";
    13. out.flush();
    14. QTextStream in(stdin);
    15. out.flush();
    16. search_word = in.readLine(); /// here new word to insert on class..
    17. out << "Your word:" << search_word << "\n";
    18. out << str.fill('*', 80) << "\n";
    19.  
    20. /// return 1; or
    21. QTimer::singleShot(10000, &app, SLOT(quit()));
    22. return app.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 

    Last edited by patrik08; 29th September 2013 at 17:06.

  26. The following 2 users say thank you to patrik08 for this useful post:


  27. #14
    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?

    Quote Originally Posted by anda_skoa View Post
    Just checking: you have tried, lets say my first suggestion, right?
    I.e. using toLocal8Bit() and it did not work?

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication a(argc, argv);
    7.  
    8. qDebug() << QString::fromUtf8("Привет Мир").toLocal8Bit();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _
    Thank you. It doesn't work.


    Added after 19 minutes:


    Why it doesn't work?

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDebug>
    3. #include <QTextStream>
    4.  
    5. QTextStream cout(stdout);
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QCoreApplication a(argc, argv);
    10.  
    11. QString russian = QString::fromUtf8("Привет, Мир!");
    12. qDebug() << russian;
    13. cout << russian << endl;
    14. cout.flush();
    15.  
    16. return a.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 

    Output:
    "╧ЁштхЄ, ╠шЁ!"
    ╧ЁштхЄ, ╠шЁ!

    Added after 4 minutes:


    I don't understand what is it:

    Привет, Мир!
    "Привет,"
    Привет,
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDebug>
    3. #include <QTextStream>
    4.  
    5. QTextStream cin(stdin);
    6. QTextStream cout(stdout);
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QCoreApplication a(argc, argv);
    11.  
    12. QString input;
    13. cin >> input;
    14. QString russian = QString::fromUtf8(input.toUtf8());
    15. qDebug() << russian;
    16. cout << russian << endl;
    17. cout.flush();
    18.  
    19. return a.exec();
    20. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by 8Observer8; 8th October 2013 at 11:32.

  28. The following 2 users say thank you to 8Observer8 for this useful post:


  29. #15
    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?

    I don't know , nothing solved my problem which is exactly the same as yours.
    too bad we cant find a solution for it.

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


  31. #16
    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?

    I found the solution

    main.cpp
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QTextCodec>
    3. #include <QTextStream>
    4.  
    5. void sayhellow(const QString& s) {
    6. QTextStream out(stdout);
    7. #if defined(Q_WS_WIN)
    8. out.setCodec("IBM866");
    9. #endif
    10. out << s << endl;
    11. }
    12.  
    13. int main(int argc, char *argv[])
    14. {
    15. QCoreApplication a(argc, argv);
    16. QTextCodec * codec;
    17. codec = QTextCodec::codecForName("utf-8");
    18. QTextCodec::setCodecForCStrings(codec);
    19. QTextCodec::setCodecForLocale(codec);
    20. QTextCodec::setCodecForTr(codec);
    21.  
    22. QString s = "Привет, Мир!";
    23. sayhellow(s);
    24.  
    25. return a.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  32. The following 2 users say thank you to 8Observer8 for this useful post:


  33. #17
    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?

    @ 8Observer8

    Thanks for trying again!
    I am using Qt 5.1.
    What Qt version are you using because setCodecForCStrings and setCodecForTr is not a member of QTextCodec.
    Unfortunately this doesnt solve my problem.
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QTextCodec>
    3. #include <QTextStream>
    4.  
    5. void sayhellow(const QString& s) {
    6. QTextStream out(stdout);
    7. out << s << endl;
    8. }
    9.  
    10. int main(int argc, char *argv[])
    11. {
    12. QCoreApplication a(argc, argv);
    13. QTextCodec * codec;
    14. codec = QTextCodec::codecForName("CP-1252");
    15. QTextCodec::setCodecForLocale(codec);
    16. QString s = "أدخل رمز";
    17. sayhellow(s);
    18.  
    19. return a.exec();
    20. }
    To copy to clipboard, switch view to plain text mode 
    still prints ???? ???

    If anyone knows how to solve this problem please share with us.

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


  35. #18
    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?

    I use Qt 4.8.5


    Added after 4 minutes:


    toufic.dbouk, install Qt 4.8.5 for experiment )
    Last edited by 8Observer8; 27th November 2013 at 18:31.

  36. The following 2 users say thank you to 8Observer8 for this useful post:


  37. #19
    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 is a hard request but i will try to install Qt 4.8.5 on another laptop and test it.
    But since you already have that version installed try printing my string mentioned in the above post and see the result.

    Good Luck.

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


  39. #20
    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?

    Quote Originally Posted by toufic.dbouk View Post
    @ 8Observer8

    Thanks for trying again!
    I am using Qt 5.1.
    What Qt version are you using because setCodecForCStrings and setCodecForTr is not a member of QTextCodec.
    Unfortunately this doesnt solve my problem.
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QTextCodec>
    3. #include <QTextStream>
    4.  
    5. void sayhellow(const QString& s) {
    6. QTextStream out(stdout);
    7. out << s << endl;
    8. }
    9.  
    10. int main(int argc, char *argv[])
    11. {
    12. QCoreApplication a(argc, argv);
    13. QTextCodec * codec;
    14. codec = QTextCodec::codecForName("CP-1252");
    15. QTextCodec::setCodecForLocale(codec);
    16. QString s = "أدخل رمز";
    17. sayhellow(s);
    18.  
    19. return a.exec();
    20. }
    To copy to clipboard, switch view to plain text mode 
    still prints ???? ???

    If anyone knows how to solve this problem please share with us.
    Thank you very much. You are right! It doesn't work in Qt5! Please, somebody, help us!!!

  40. The following 2 users say thank you to 8Observer8 for this useful post:


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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.