0

I have a QList contains a list of OpenCv::Mat. I'd like to write the QList to a file and read a file into a QList with QDataStream.

Here is part of my code:

// Variables declarations
QList<Mat> myList;
cv::Mat someData;
QString fileName = "list.dat";
QFile file(fileName);
QDataStream out(&file);
QDataStream in(&file);

// Storing some data into the list
myList.append(someData);
myList.append(someData);
myList.append(someData);

// Writing the list to a file
file.open(QIODevice::WriteOnly);
out << myList;
file.close();

// Reading a file to the list
file.open(QIODevice::ReadOnly);
in >> myList;
file.close();

And I received two errors:

error: C2678: binary '>>': no operator found which takes a left-hand operand of type 'QDataStream' (or there is no acceptable conversion)

error: C2678: binary '<<': no operator found which takes a left-hand operand of type 'QDataStream' (or there is no acceptable conversion)

Any suggestions?

  • Possible duplicate of [saving and loading vector Qt & OpenCV](http://stackoverflow.com/questions/26073613/saving-and-loading-vectormat-qt-opencv) – eyllanesc Apr 26 '17 at 08:40
  • `QDataStream & operator>>()` has no overload for `QList`. you have to push_back (or so) in a loop... – user1810087 Apr 26 '17 at 08:43
  • You must provide the two operators your self, they should have a signature like these: `QDataStream& operator>>(QDataStream&, const QList&);` and `QDataStream& operator<<(QDataStream&, const QList&);` – Jonas Apr 26 '17 at 08:43
  • You only need operators for the Mat type, Qt knows how to serialize QList. – dtech Apr 26 '17 at 09:47

1 Answers1

1

You need to provide those two operators for your custom type Mat :

QDataStream & operator<<(QDataStream &out, const Mat& mat)
QDataStream & operator>>(QDataStream &in, Mat& mat)
Fryz
  • 2,119
  • 2
  • 25
  • 45
  • And write `return out << mat;` `return in >> mat;` inside of two functions? – Richard Song Apr 26 '17 at 11:02
  • Actually, you should de/serialize your object the way you want into the `QDataStream` and then call `return in` and `return out.` You have some examples here in the Qt doc http://doc.qt.io/qt-4.8/qdatastream.html#details . And here is en example of class serialization http://stackoverflow.com/questions/2570679/serialization-with-qt using QDataStream – Fryz Apr 26 '17 at 12:14