1

I can write a QVariant to a QDataStream and read a QVariant from a QDataStream without a problem.

QByteArray byteArray;

QDataStream outStream(&byteArray, QIODevice::WriteOnly);
QVariant outVar(QString("hello"));
outStream << outVar;

QVariant inVar;
QDataStream inStream(&byteArray, QIODevice::ReadOnly);
inStream >> inVar;

My problem is, how does above code compile and work properly when QDataStream does not have method operator<<(QVariant v) ?

Lahiru Chandima
  • 22,324
  • 22
  • 103
  • 179

1 Answers1

2

Because the operator<< is declared in QVariant.

According to QDataStream,

In addition to the overloaded stream operators documented here, any Qt classes that you might want to serialize to a QDataStream will have appropriate stream operators declared as non-member of the class.

In the source code of QVariant, you can find the two non-member functions (unfortunately, they are not listed in the documentation of QVariant) :

QDataStream& operator<<(QDataStream &s, const QVariant &p)
QDataStream& operator>>(QDataStream &s, QVariant &p)
mcchu
  • 3,309
  • 1
  • 20
  • 19