在Ubuntu Focal上使用Qt 5.12时出现此错误。但是,当我使用Qt 5.13.2及更高版本时,未显示任何错误。我想让它在5.12及更低版本中工作,该怎么做呢?

TQProfile::TQProfile()
{
    type = "abc";
    abcSettings = ConfigHelper::generateAbcSettings();
}

QDataStream& operator << (QDataStream &out, const TQProfile &p)
{
    out << p.type << p.abcSettings;
    return out;
}

QDataStream& operator >> (QDataStream &in, TQProfile &p)
{
    in >> p.type >> p.abcSettings;
    return in;
}

struct TQProfile
{
    TQProfile();
    QString type;
    QJsonObject abcSettings;
};
Q_DECLARE_METATYPE(TQProfile)

QDataStream& operator << (QDataStream &out, const TQProfile &p);
QDataStream& operator >> (QDataStream &in, TQProfile &p);

最佳答案

一个简单的解决方案是使用QJsonDocument将QJsonObject转换为QByteArray:

QDataStream& operator << (QDataStream &out, const TQProfile &p)
{
    QJsonDocument doc(p.abcSettings);
    out << p.type << doc.toJson(QJsonDocument::Compact);
    return out;
}

QDataStream& operator >> (QDataStream &in, TQProfile &p)
{
    QByteArray data;
    in >> p.type >> data;
    p.abcSettings = QJsonDocument::fromJson(data).object();
    return in;
}

10-08 16:20