天道酬勤,学无止境

qdatastream

How to serialize a QAbstractItemModel into QDataStream?

Question I've set up a QAbstractItemModel and filled that with data. My QTreeView widget displays every data in that model properly. Now, I would like to store that model serialized in a binary file (and later of cource load that binary file back into a model). Is that possible? Answer1 The particulars of model serialization depend somewhat on the model's implementation. Some gotchas include: Perfectly usable models might not implement insertRows/insertColumns, preferring to use custom methods instead. Models like QStandardItemModel may have underlying items of varying types. Upon

2021-12-12 08:14:39    分类:技术分享    qt   serialization   qabstractitemmodel   qstandarditemmodel   qdatastream

saving and loading vector<Mat> Qt & OpenCV

Question I'm working on face recognition in Qt & openCV using the FisherFaces recognizer which doesn't support updating so i have to save the faces database to retrain the recognizer after any changes. Here is my code for saving : save(const std::vector* MatVect){ QFile file("students_dataset.dat"); file.open(QIODevice::WriteOnly); QDataStream out(&file); QVector qimgvect; for (size_t i = 0; i < MatVect->size(); ++i) { cv::Mat matt = MatVect->at(i); QImage img((uchar*)matt.data, matt.cols, matt.rows, matt.step, QImage::Format_Indexed8); qimgvect.push_back(img); } out << qimgvect ; file.flush()

2021-11-19 19:04:23    分类:技术分享    qt   opencv   mat   qimage   qdatastream

QDataStream unable to serialize data

Question I am trying to follow the tutorial here and serialize Qt objects. Here is my code: QFile file("/Users/kaustav/Desktop/boo.dat"); if (!file.open(QIODevice::WriteOnly)) { qDebug() << "Cannot open file for writing: " << qPrintable(file.errorString()) << endl; //no error message gets printed return 0; } QDataStream out(&file); // we will serialize the data into the file out.setVersion(QDataStream::Qt_5_3); //adding this makes no difference out << QString("the answer is"); // serialize a string out << (qint32)42; When I run this program, the file gets created in my desktop all right, but

2021-11-10 05:52:25    分类:技术分享    c++   qt   persistence   object-serialization   qdatastream

Converting QImage to QByteArray using QDataStream

Question Im trying to convert a QImage that maked from ScreenShot to a QByteArray for sending via QTCPSocket. when i convert QImage to QByteArray and before sending it i try to deserialize and show it on label it cant ! what's my mistake? thx for helping. QByteArray ImClientShooter::toQByteArray(QImage &img) { QByteArray temp; QDataStream data(&temp, QIODevice::ReadWrite); data « img; return temp; } QByteArray goOn{toQByteArray(sampleQImage)}; //sampleQImage is a QImage Object lbl->setPixmap(QPixmap::fromImage( (QImage::fromData(goOn)))); // QLabel* lbl sampleQImage maked from ScreenShot

2021-10-27 00:05:08    分类:技术分享    c++   qt   qimage   qbytearray   qdatastream

Reading specific object in QDataStream and count number of objects stored

Question I am writting some objects in a binary file and I would like to read them back. To explain you what I am trying to do, I prepared a simple example with a class User that contains the QString name and QList name of childrens. Please see the code below. #include "QString" #include "QFile" #include "QDataStream" #include "qdebug.h" class User { protected: QString name; QList<QString> childrens; public: QString getName(){ return name;} QList<QString> getChildrens(){ return childrens;} void setName(QString x) {name = x;} void setChildrens(QList<QString> x) {childrens = x;} //I have no idea

2021-10-26 09:36:04    分类:技术分享    c++   qt   binaryfiles   qdatastream

Qt - QDataStream with overloaded operator<< for object pointer (Class*)

Question I am trying to read/write my custom classes using QDataStream. I have overridden the << and >> operators, which seem to work fine for normal objects. However, when I try to pass a pointer to my custom object, the overridden operators don't work properly. Here is the relevant data from card.h: #ifndef CARD_H #define CARD_H #include <QDataStream> #include <QImage> #include <QString> class Card { private: QString name; QImage image; QString type; int strength; int movement; int deployCost; QString back; public: Card(); QDataStream& read(QDataStream &dataStream); QDataStream& write

2021-10-14 14:07:37    分类:技术分享    c++   qt   operator-overloading   qdatastream

Append a QByteArray to QDataStream?

Question I have to populate a QByteArray with different data. So I'm using the QDataStream. QByteArray buffer; QDataStream stream(&buffer, QIODevice::WriteOnly); qint8 dataHex= 0x04; qint8 dataChar = 'V'; stream << dataHex<< dataChar; qDebug() << buffer.toHex(); // "0456" This is what I want However, I would also like to append a QByteArray to the buffer. QByteArray buffer; QDataStream stream(&buffer, QIODevice::WriteOnly); qint8 dataHex= 0x04; qint8 dataChar = 'V'; QByteArray moreData = QByteArray::fromHex("ff"); stream << dataHex<< dataChar << moreData.data(); // char * QByteArray::data ()

2021-08-30 18:57:47    分类:技术分享    c++   qt   qbytearray   qdatastream

Save and load QList<Class*> to file

Question I have a class ContactData and a class FriendList holding a QList and I overloaded the << / >> operators. contactdata.h class ContactData { //all public for testing public: ContactData(); QString m_name; QString m_description; bool m_online; }; QDataStream &operator<<(QDataStream& out, ContactData& contactData); QDataStream &operator>>(QDataStream& in, ContactData* contactData); contactdata.cpp QDataStream &operator<<(QDataStream &out, ContactData& contactData) { out << contactData.m_name; out << contactData.m_description; return out; } QDataStream &operator>>(QDataStream &in

2021-08-14 05:04:41    分类:技术分享    c++   qt   qlist   qdatastream

How to open a bin file in Python using QDataStream

Question I've got a bin file that was encoded in an application that I need to get access to and convert to a csv file. I've been given the documentation, but am not sure how to access the contents of this file in Python. Here are some of the details about how the dataset was serialized Datasets.bin is a list of DataSet classes serialized using Qt's QDataStream serialization using version QDataStream::Qt_4_7. The format of the datasets.bin file is: quint32 Magic Number 0x46474247 quint32 Version 1 quint32 DataSet Marker 0x44415441 qint32 # of DataSets n DataSet DataSet 1 DataSet DataSet 2 . .

2021-06-26 00:47:39    分类:技术分享    python   qt   csv   pyqt   qdatastream