- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我为自定义类重载了 QDatastream 的 >> 和 << 运算符。我制作了 2 个版本;一个用于基础对象,一个用于对象的指针。到目前为止,所有版本的运算符都可以使用一个异常(exception)。指针读取操作符读取正确的数据,但未正确保存在 QList<*> 中。
这是一些示例代码。
QDataStream & operator <<(QDataStream &dataStream, const Faction &rhs)
{
return rhs.write(dataStream);
}
QDataStream & operator >>(QDataStream &dataStream, Faction &rhs)
{
return rhs.read(dataStream);
}
QDataStream & operator <<(QDataStream &dataStream, const Faction *rhs)
{
return rhs->write(dataStream);
}
QDataStream & operator >>(QDataStream &dataStream, Faction *rhs)
{
rhs = new Faction();
return rhs->read(dataStream);
}
QDataStream & Faction::read(QDataStream &dataStream)
{
QString tag;
dataStream >> tag;
QString classTag = QString(typeid(this).name());
getTypeName(&classTag);
if (tag == classTag + "Start")
{
while (tag != classTag + "End")
{
if (tag == "name")
{
dataStream >> name; // The name of the faction.
}
else if (tag == "buildings")
{
dataStream >> buildings; // The buildings of the Faction.
}
else if (tag == "units")
{
dataStream >> units; // The units of the Faction.
}
else if (tag == "upgrades")
{
dataStream >> upgrades; // The upgrades of the Faction.
}
else if (tag == "startBuildings")
{
dataStream >> startBuildings; // The list of buildings when starting a game.
}
else if (tag == "startUnits")
{
dataStream >> startUnits; // The list of units when starting a game.
}
else if (tag == "startUpgrades")
{
dataStream >> startUpgrades; // The list of upgrades when starting a game.
}
// Reading the next tag.
dataStream >> tag;
}
}
return dataStream;
}
QDataStream & Faction::write(QDataStream &dataStream) const
{
QString classTag = QString(typeid(this).name());
getTypeName(&classTag);
dataStream << QString(classTag + "Start");
dataStream << QString("name");
dataStream << name; // The name of the faction.
dataStream << QString("buildings");
dataStream << buildings; // The buildings of the Faction.
dataStream << QString("units");
dataStream << units; // The units of the Faction.
dataStream << QString("upgrades");
dataStream << upgrades; // The upgrades of the Faction.
dataStream << QString("startBuildings");
dataStream << startBuildings; // The list of buildings when starting a game.
dataStream << QString("startUnits");
dataStream << startUnits; // The list of units when starting a game.
dataStream << QString("startUpgrades");
dataStream << startUpgrades; // The list of upgrades when starting a game.
dataStream << QString(classTag + "End");
return dataStream;
}
#ifndef FACTION_H
#define FACTION_H
#include <JECUtils.h>
#include <Unit.h>
#include <UnitBase.h>
class Faction
{
public:
explicit Faction();
explicit Faction(const QString& name);
Faction(const Faction& faction);
Faction& operator=(const Faction& rhs);
bool operator==(const Faction& rhs) const;
bool operator!=(const Faction& rhs) const;
friend QDataStream &operator<<(QDataStream &dataStream, const Faction& rhs);
friend QDataStream &operator>>(QDataStream &dataStream, Faction& rhs);
friend QDataStream &operator<<(QDataStream &dataStream, const Faction* rhs);
friend QDataStream &operator>>(QDataStream &dataStream, Faction* rhs);
void addBuilding(UnitBase* building);
void addUnit(UnitBase* unit);
void addUpgrade(UnitBase* upgrade);
const QString& getName() const;
const UnitBase* getBuilding(const int& index) const;
const QList<UnitBase*>& getBuildings() const;
const UnitBase* getUnit(const int& index) const;
const QList<UnitBase*>& getUnits() const;
const UnitBase* getUpgrade(const int& index) const;
const QList<UnitBase*>& getUpgrades() const;
const QList<QList<Unit*> >* getStartUnits() const;
const QList<QList<Unit*> >* getStartBuildings() const;
const QList<QList<Unit*> >* getStartUpgrades() const;
void initialize(const QStringList& globalActions);
void removeAllBuilding();
void removeAllUnit();
void removeAllUpgrade();
void removeBuilding(const int& index);
void removeUnit(const int& index);
void removeUpgrade(const int& index);
void setStartUp(const QStringList& names, const QList<int>& quantities);
protected:
QDataStream& read(QDataStream &dataStream);
QDataStream& write(QDataStream &dataStream) const;
private:
QString name; // The name of the faction.
QList<UnitBase*> buildings; // The buildings of the Faction.
QList<UnitBase*> units; // The units of the Faction.
QList<UnitBase*> upgrades; // The upgrades of the Faction.
QList<QList<Unit*> > startBuildings; // The list of buildings when starting a game.
QList<QList<Unit*> > startUnits; // The list of units when starting a game.
QList<QList<Unit*> > startUpgrades; // The list of upgrades when starting a game.
};
#endif // FACTION_H
dataStream >> factions
QDataStream & operator >>(QDataStream &dataStream, Faction *rhs)
{
rhs = new Faction();
return rhs->read(dataStream); <---- rhs will return good data.
}
最佳答案
运算符 >> 需要一个引用作为其第二个参数,您也可以引用指针:
QDataStream & operator >>(QDataStream &dataStream, Faction *&rhs)
{
rhs = new Faction();
return rhs->read(dataStream);
}
关于qt - QDatastream operator>> 用于 QList<Class*>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7381843/
我对 Qt Qlist 容器有一个有趣的问题。尝试将 QList append 到 QList 会使我的迭代器指向内存的未知部分。 QList listSmall; QList> listBig; f
我对 documentation. 的 QList 复制构造函数感到困惑 QList::QList ( const QList & other ) Constructs a copy of other
我试图在运行时将项目附加到 QList,但运行时出现错误消息。基本上我想做的是制作一个 QList 的 QList 并向每个内部列表添加一些 customClass 对象。这是我的代码: 小部件.h:
所以我有一个名为 AppointmentSchedule 的类,它属于以下类型: namespace Ui { class AppointmentSchedule; } class Appointme
我正在使用 Qt5 用 C++ 编写一个视频抓取应用程序。我正在关注他们的示例代码并查看获取相机信息的文档: http://doc.qt.io/qt-5/qcamerainfo.html 我遇到的问题
我试图将 2d QList 作为 Q_PROPERTY 传递到 QML,但是,在 QML 内部,我无法实际访问任何信息。 一些代码: C++:q_property 由构造函数中的 q_invokabl
我有以下方法(只有 1 个参数),我想对其进行调整以用于 1 个以上的参数。我尝试使用默认参数,但这不起作用。 (只有旧方法的原始代码工作正常)令我印象深刻的是该声明中省略了变量名。 为什么? 这是我
在我的 mainwindow.h 中,我有一个 QList m_qlServoList,它应该存储指向 Servo 对象的指针: QList m_qlServoList; 当我尝试将新的伺服指针 ap
有没有简单的方法来复制QList从新 QList 中的位置 a 到 b? 我测试过: QList newList(list.begin()+5,list.end()); 但它不起作用。我收到错误消
我找不到使用另一个类模板将 QObjects 的 QList 转换为另一个 QList 的方法。我有一个创建 QObjects 实例然后返回 QList 的函数。所以我的问题是,我可以将 QList
有没有简单的方法来创建 QList来自 QList ? (不迭代 QList 并将每个元素添加到 QList ) 最佳答案 答案是否定的。你怎么可能在不迭代的情况下将一个转换成另一个?即使您正在使用某
我有几个属性类型为 QList 的类.我使用原始指针来表示所有权。此访问器将返回 QList>因为我们不想分发原始指针。问题是目前每个访问器或多或少看起来像下面这样: QList> values; C
我正在尝试使用 QT 框架获取存储在 QList 中的帐户列表的总余额。 我的问题是总余额需要从不允许我这样做的类中访问 protected 成员余额。 这个问题来自大学的作业,问题已经给了我程序的
学习C++,所以要温柔:)... 我一直主要使用堆变量(来自 C)来设计我的应用程序,所以我设计了这样的结构: QList _Criteria; // ... Criteria *c = new Cr
因此主题行中提到的代码会导致 Qt 4.8.3 和 gcc 4.7.2 出现段错误 这在 .cpp 文件中的任何类/结构之外,适用于 gcc 4.4 const QList warnings = QL
我最近一直在从事一个项目。当我尝试运行项目时,出现此错误: /ASSERT failure in QList::operator[]: "index out of range", file /usr/
我对 QT 没有太多经验,今天出现了这个问题。 QList memList; const int large = 100000; getchar(); for (int i=0; i
我明白了qCopy ,和 qCopybackward但似乎都不让我以相反的顺序复印。 qCopybackward 仅以相反的顺序复制它,但使该死的元素保持相同的顺序!我想做的就是以相反的顺序返回列表的
typedef struct { char *u8_testStep; char *u8_functionTested; char *u8_testDescription;
我有一个应用程序,其目的是获取按键值并将其放入 QList 中。但是,每次我调用处理 QList 修改的类成员函数时,成员变量 QList 都会被重置。我相信我有范围问题,但我不知道在哪里。这是我的代
我是一名优秀的程序员,十分优秀!