重载QDataStream<<和>>用户定义类型的运算符 [英] Overloading the QDataStream << and >> operators for a user-defined type
问题描述
我有一个对象,我想能够读/写一个QDataStream。标题如下:
I have a an object I'd like to be able to read and write to/from a QDataStream. The header 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;
};
对于所有意图和目的,类只是作为少数QStrings和QPixmap的抽象。理想情况下,我可以写一个QList到QDataStream,但我不知道如何去做这个。
For all intents and purposes, the class just serves as an abstraction for a handful of QStrings and a QPixmap. Ideally, I would be able to write a QList to a QDataStream but I'm not exactly sure how to go about doing this.
如果操作符重载是一个合适的解决方案,将编写代码
If operator overloading is a suitable solution, would writing code like
friend QDataStream& operator << (QDataStream&,Compound) { ... }
我非常开放的建议!请让我知道是否需要进一步澄清。
be a potential solution? I'm very open to suggestions! Please let me know if any further clarification is needed.
推荐答案
我认为你已经回答了你自己的问题!流运算符
I think you've answered your own question! The stream operator
QDataStream& operator<<( QDataStream&, const Compound& )
在实现中,您只需使用 QDataStream
上的现有流运算符来序列化您的复合
的各个位。一些Qt类定义非成员 QDataStream
运算符。 QString
是一个,因此 QList
所以看起来像你排序!
will work fine. In the implementation you just use the existing stream operators on QDataStream
to serialise the individual bits of your Compound
. Some Qt classes define non-member QDataStream
operators too. QString
is one and so is QList
so it looks like you're sorted!
这篇关于重载QDataStream<<和>>用户定义类型的运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!