Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator << for QString

Tags:

c++

qt

qstring

It makes sense to implement << for QString like:

std::ostream&  operator <<(std::ostream &stream,const QString &str)
{
   stream << str.toAscii().constData(); //or: stream << str.toStdString(); //??
   return stream;
}

instead of writing

stream << str.toAscii().constData();

every time in the code.

However, since it is not in standard Qt library, I'm assuming there is any particular reason not to do so. What are the risks/inconvenience of overloading << as specified above?

like image 666
Ilya Kobelevskiy Avatar asked Apr 29 '13 14:04

Ilya Kobelevskiy


People also ask

What is the use of QString operator+?

const QStringoperator+ ( char c, const QString & s ) This is an overloaded member function, provided for convenience. It behaves essentially like the above function. Returns a string which is the result of concatenating the character cand string s.

What is QString class in Java?

The QString class provides a Unicode character string. QString stores a string of 16-bit QChar s, where each QChar corresponds one Unicode 4.0 character. (Unicode characters with code values above 65535 are stored using surrogate pairs, i.e., two consecutive QChar s.)

How do I convert a string to a number in QString?

QString provides many functions for converting numbers into strings and strings into numbers. See the arg() functions, the setNum() functions, the number() static functions, and the toInt(), toDouble(), and similar functions. To get an upper- or lowercase version of a string use toUpper() or toLower().

What is qstringbuilder in Qt?

Qt 4.6 introduces an internal class called QStringBuilder that "reserves" memory for a concatenation chain in a single shot. It does so by having each of the + operations above return a different class (not QString anymore). This class keeps track of the string's that are being appended and the required memory at each step.


4 Answers

If the << operator is included in the Qt library every client of the library will have to use the exact same implementation. But due to the nature of QString it is far from obvious this is what these clients want. Some people writing software interacting with legacy file in western europe may want to use Latin1() characters, US people may go with Ascii() and more modern software may want to use Utf8().

Having a single implementation in the library would restrict unacceptably what can be done with the whole library.

like image 177
fjardon Avatar answered Oct 11 '22 19:10

fjardon


It's not necessary to implement such thing, as long as there exists a convenient solution like this one, involving QTextStream

QString s;
QTextStream out(&s);
out << "Text 1";
out << "Text 2";
out << "And so on....";

QTextStream is quite powerfull...

like image 28
BlueAutumn Avatar answered Oct 11 '22 17:10

BlueAutumn


The accepted answer points out some valid reasons for why there is no operator<< function for QString.

One can easily overcome those reasons by providing some convenience functions and maintaining some state in an application specific namespace.

#include <iostream>
#include <QString>

namespace MyApp
{
   typedef char const* (*QStringInsertFunction)(QString const& s);

   char const* use_toAscii(QString const& s)
   {
      return s.toAscii().constData();
   }

   char const* use_toUtf8(QString const& s)
   {
      return s.toUtf8().constData();
   }

   char const* use_toLatin1(QString const& s)
   {
      return s.toLatin1().constData();
   }

   // Default function to use to insert a QString.
   QStringInsertFunction insertFunction = use_toAscii;

   std::ostream& operator<<(std::ostream& out, QStringInsertFunction fun)
   {
      insertFunction = fun;
      return out;
   }

   std::ostream& operator<<(std::ostream& out, QString const& s)
   {
      return out << insertFunction(s);
   }
};

int main()
{
   using namespace MyApp;

   QString testQ("test-string");

   std::cout << use_toAscii << testQ << std::endl;
   std::cout << use_toUtf8 << testQ << std::endl;
   std::cout << use_toLatin1 << testQ << std::endl;

   return 0;
}

Output:

test-string
test-string
test-string
like image 42
R Sahu Avatar answered Oct 11 '22 17:10

R Sahu


I don't think there is any particular reason for excluding (nor including) this in the Qt library. Only problem that could possibly appear here is a possibility that std::ostream object could modify the contents of the parameter passed to std::ostream::operator<< function.

However, in the reference it is clearly stated that this function will modify the parameter if string buffer is passed - there is nothing about the other types, so I guess (and the common-sense is telling me) that operator<< will not modify char* parameter. Also, on this page there is nothing about modifying the passed object.

Last thing: instead of using QString::toAscii().constData(), you could use QString::toStdString() or qPrintable(const QString&) macro.

like image 26
Nemanja Boric Avatar answered Oct 11 '22 19:10

Nemanja Boric