我有一个这样的自定义结构:
struct aReminderStruct{
QString name;
QDate date;
QTime time;
QString reminderText;
};
aReminderStruct myNewReminder;
填充数据后,如何将
myNewReminder
转换为QByteArray
? 最佳答案
您需要像这样定义自定义数据流运算符:
struct aReminderStruct{
QString name;
QDate date;
QTime time;
QString reminderText;
};
// you need this if you want to use your type with QVariant
Q_DECLARE_METATYPE(aReminderStruct)
QDataStream &operator<<(QDataStream &out, const aReminderStruct &a)
{
out << name << date << time << reminderText;
return out;
}
QDataStream &operator>>(QDataStream &in, aReminderStruct &a)
{
in >> name >> date >> time >> reminderText;
return in;
}
...
int main(...)
{
QApplication app(...);
qRegisterMetaTypeStreamOperators<aReminderStruct>("aReminderStruct");
...
aReminderStruct a;
a = ...;
QByteArray data;
QDataStream ds(&data, QIODevice::ReadWrite);
ds << a;
}