How to write a Russian text in console?
Hi,
How to write a Russian text in console?
This is my code:
Code:
#include <QCoreApplication>
#include <QDebug>
#include <QTextCodec>
int main(int argc, char *argv[])
{
qDebug
() <<
QObject::tr("Привет Мир");
return a.exec();
}
Output:
Quote:
"╨Я╤Ð╨╕╨■“╨╡╤В ╨Ь╨╕╤Ð"
Thank you!
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,
_
Re: How to write a Russian text in console?
Code:
#include <QCoreApplication>
#include <QDebug>
#include <QTextCodec>
#include <QTextStream>
int main(int argc, char *argv[])
{
cout <<
QObject::tr("Привет Мир");
cout.flush();
return a.exec();
}
Output:
Quote:
╨Я╤Ð╨╕╨■╨╡╤В ╨Ь╨╕╤Ð
Re: How to write a Russian text in console?
That is essentially the same thing.
Code:
cout <<
QObject::tr("Привет Мир");
Since there is no operator<<(std::ostream&, 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,
_
Re: How to write a Russian text in console?
Code:
#include <QCoreApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
qDebug
() <<
QString::fromUtf8("Привет Мир");
return a.exec();
}
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.
Re: How to write a Russian text in console?
It doesn't work:
Code:
#include <QCoreApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
qDebug
() <<
QString::fromUtf8("Привет Мир");
return a.exec();
}
Output:
Quote:
"â•§ÐштхЄ ╠шÐ"
I will read about it. Thank you.
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.
Code:
int len = str.size();
int chk;
for( int i = 0; i < len; i++ )
{
chk = str.at(i).unicode();
}
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.
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.
1 Attachment(s)
Re: How to write a Russian text in console?
Yes! It works! I will be trying yet.
Attachment 9645
Code:
#include <QApplication>
#include <QtGui>
int main(int argc, char *argv[]) {
lblText->show();
return app.exec();
}
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).
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?
Code:
#include <QCoreApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
qDebug
() <<
QString::fromUtf8("Привет Мир").
toLocal8Bit();
return a.exec();
}
Cheers,
_
Re: How to write a Russian text in console?
Quote:
using toLocal8Bit() and it did not work?
.
Code:
qDebug
() <<
QString::fromUtf8("/*some Arabic word*/").
toLocal8Bit();
in my case ( trying to show Arabic alphabets ) it just shows * ??? * instead of the actual letters.
Just want to point this out.
Re: How to write a Russian text in console?
untested you can try...
or set
setCodecForTr(russian); to out && in
Code:
int main(int argc, char *argv[]) {
/// QTextCodec *russian = QTextCodec::codecForName("CP1251");
/// or out.setCodecForTr(russian);
out << str.fill('*', 80) << "\n";
out.flush();
out << "Please enter word to search russian keyboard:\n";
out.flush();
out.flush();
search_word = in.readLine(); /// here new word to insert on class..
out << "Your word:" << search_word << "\n";
out << str.fill('*', 80) << "\n";
/// return 1; or
QTimer::singleShot(10000,
&app,
SLOT(quit
()));
return app.exec();
}
Re: How to write a Russian text in console?
Quote:
Originally Posted by
anda_skoa
Just checking: you have tried, lets say my first suggestion, right?
I.e. using toLocal8Bit() and it did not work?
Code:
#include <QCoreApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
qDebug
() <<
QString::fromUtf8("Привет Мир").
toLocal8Bit();
return a.exec();
}
Cheers,
_
Thank you. It doesn't work.
Added after 19 minutes:
Why it doesn't work?
Code:
#include <QCoreApplication>
#include <QDebug>
#include <QTextStream>
int main(int argc, char *argv[])
{
qDebug() << russian;
cout << russian << endl;
cout.flush();
return a.exec();
}
Output:
Quote:
"â•§ÐштхЄ, ╠шÐ!"
â•§ÐштхЄ, ╠шÐ!
Added after 4 minutes:
I don't understand what is it:
Quote:
Привет, Мир!
"Привет,"
Привет,
Code:
#include <QCoreApplication>
#include <QDebug>
#include <QTextStream>
int main(int argc, char *argv[])
{
cin >> input;
qDebug() << russian;
cout << russian << endl;
cout.flush();
return a.exec();
}
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.
1 Attachment(s)
Re: How to write a Russian text in console?
I found the solution :)
main.cpp
Code:
#include <QCoreApplication>
#include <QTextCodec>
#include <QTextStream>
void sayhellow(const QString& s) {
#if defined(Q_WS_WIN)
out.setCodec("IBM866");
#endif
out << s << endl;
}
int main(int argc, char *argv[])
{
QString s
= "Привет, Мир!";
sayhellow(s);
return a.exec();
}
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.
Code:
#include <QCoreApplication>
#include <QTextCodec>
#include <QTextStream>
void sayhellow(const QString& s) {
out << s << endl;
}
int main(int argc, char *argv[])
{
sayhellow(s);
return a.exec();
}
still prints ???? ???
If anyone knows how to solve this problem please share with us.
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 )
Re: How to write a Russian text in console?
That is a hard request :p 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.
Re: How to write a Russian text in console?
Quote:
Originally Posted by
toufic.dbouk
@ 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.
Code:
#include <QCoreApplication>
#include <QTextCodec>
#include <QTextStream>
void sayhellow(const QString& s) {
out << s << endl;
}
int main(int argc, char *argv[])
{
sayhellow(s);
return a.exec();
}
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!!!