Overloading QDataStream << and >> operators for a custom type

I have an object that I would like to read and write to / from QDataStream. The title is as follows:

 class Compound { public: Compound(QString, QPixmap*, Ui::MainWindow*); void saveCurrentInfo(); void restoreSavedInfo(QGraphicsScene*); void setImage(QPixmap*); QString getName(); private: QString name, homeNotes, addNotes, expText; Ui::MainWindow *gui; QPixmap *image; struct NMRdata { QString hnmrText, cnmrText, hn_nmrText, hn_nmrNucl, notes; int hnmrFreqIndex, cnmrFreqIndex, hn_nmrFreqIndex, hnmrSolvIndex, cnmrSolvIndex, hn_nmrSolvIndex; }*nmr_data; struct IRdata { QString uvConc, lowResMethod, irText, uvText, lowResText, highResText, highResCalc, highResFnd, highResFrmla, notes; int irSolvIndex, uvSolvIndex; }*ir_data; struct PhysicalData { QString mpEdit, bpEdit, mpParensEdit, bpParensEdit, rfEdit, phyText, optAlpha, optConc, elemText, elemFrmla, notes; int phySolvIndex, optSolvIndex; }*physical_data; }; 

For all purposes and purposes, the class simply serves as an abstraction for several QStrings and QPixmap. Ideally, I could write a QList in a QDataStream, but I'm not quite sure how to do this.

If operator overloading is the appropriate solution, write code like

 friend QDataStream& operator << (QDataStream&,Compound) { ... } 

- a potential solution? I am very open to suggestions! Please let me know if any clarification is required.

+4
source share
2 answers

I think you answered your question! Stream operator

 QDataStream& operator<<( QDataStream&, const Compound& ) 

will work fine. In the implementation, you simply use the existing stream operators on QDataStream to serialize the individual bits of your Compound . Some Qt classes also define non-member QDataStream . QString is one, and therefore QList , so it looks like you are sorted!

+7
source

If you want to overload the extract statement →, your signature should be:

 QDataStream & operator >> (QDataStream & in, MyClass & class); 

Hope this helps.

+2
source

Source: https://habr.com/ru/post/1304504/


All Articles