output QString
-
Dear all,
For QString str,
cout << str;
produces error while compiling, and one must use str.toStdString(), but
QTextStream << str.toStdString();
produces error, and one must use str (without .toStdString()). What's the reason that Qt is designed this way? Is it not possible to design QString this way so that cout and QTextStream would have the same syntax?
Weichao -
Dear all,
For QString str,
cout << str;
produces error while compiling, and one must use str.toStdString(), but
QTextStream << str.toStdString();
produces error, and one must use str (without .toStdString()). What's the reason that Qt is designed this way? Is it not possible to design QString this way so that cout and QTextStream would have the same syntax?
Weichao -
@aha_1980
You mean that this way we can avoid using cout so that file and console output would behave the same way?
Weichao -
Dear all,
For QString str,
cout << str;
produces error while compiling, and one must use str.toStdString(), but
QTextStream << str.toStdString();
produces error, and one must use str (without .toStdString()). What's the reason that Qt is designed this way? Is it not possible to design QString this way so that cout and QTextStream would have the same syntax?
Weichao@Weichao-Wang said in output QString:
Is it not possible to design QString this way so that cout and QTextStream would have the same syntax?
You can implement this:
std::ostream& operator <<(std::ostream &stream,const QString &str) { stream << str.toAscii().constData(); //or: stream << str.toStdString(); //?? return stream; }
See https://stackoverflow.com/questions/16281014/operator-for-qstring
-
Thank you all!
Weichao -
@Weichao-Wang said in output QString:
Is it not possible to design QString this way so that cout and QTextStream would have the same syntax?
You can implement this:
std::ostream& operator <<(std::ostream &stream,const QString &str) { stream << str.toAscii().constData(); //or: stream << str.toStdString(); //?? return stream; }
See https://stackoverflow.com/questions/16281014/operator-for-qstring
@jsulm said in output QString:
stream << str.toAscii().constData();
toAscii()
is deprecated, usetoLatin1()
instead.However, note that
QString
is an Unicode string, usingtoLatin1()
will only work for letters in the Latin-1 encoding range. Most likelytoUtf8()
ortoLocal8Bit()
will be the better solution. -
@jsulm said in output QString:
stream << str.toAscii().constData();
toAscii()
is deprecated, usetoLatin1()
instead.However, note that
QString
is an Unicode string, usingtoLatin1()
will only work for letters in the Latin-1 encoding range. Most likelytoUtf8()
ortoLocal8Bit()
will be the better solution. -
Dear all,
For QString str,
cout << str;
produces error while compiling, and one must use str.toStdString(), but
QTextStream << str.toStdString();
produces error, and one must use str (without .toStdString()). What's the reason that Qt is designed this way? Is it not possible to design QString this way so that cout and QTextStream would have the same syntax?
Weichao@Weichao-Wang said in output QString:
What's the reason that Qt is designed this way?
std::string
has a terrible design. I am glad thatQString
is designed differently, because QString is much more powerful and easy to use.