c++ - 在 QObject 子类中存储一对

标签 c++ qt qt4 qml

我的问题引用问题Subclass of QObject, qRegisterMetaType, and the private copy constructor .根据它,应该可以将 QObject 的子类注册为 QVariant 可以存储的元类型。我有这段代码:

#ifndef MODELROWREFERENCE_HPP
# define MODELROWREFERENCE_HPP

#include <QObject>

#include <QDataStream>

class ModelRowReference :
  public QObject
{
  Q_OBJECT

  Q_PROPERTY(QObject* model READ model WRITE setModel)
  Q_PROPERTY(int row READ row WRITE setRow)

public:
  ModelRowReference(QObject* = 0);
  ModelRowReference(QObject*, int, QObject* = 0);

  ModelRowReference(ModelRowReference const&);

  QObject* model() const;
  void setModel(QObject*);

  int row() const;
  void setRow(int);

private:
  QObject* model_;
  int row_;
};

Q_DECLARE_METATYPE(ModelRowReference)

//////////////////////////////////////////////////////////////////////////////
inline ModelRowReference::ModelRowReference(QObject* parent) :
  QObject(parent),
  model_(0)
{
}

//////////////////////////////////////////////////////////////////////////////
inline ModelRowReference::ModelRowReference(QObject* m, int r,
  QObject* parent) :
  QObject(parent),
  model_(m),
  row_(r)
{
}

//////////////////////////////////////////////////////////////////////////////
inline ModelRowReference::ModelRowReference(ModelRowReference const& other) :
  QObject(other.parent()),
  model_(other.model_),
  row_(other.row_)
{
}

//////////////////////////////////////////////////////////////////////////////
inline QObject* ModelRowReference::model() const
{
  return model_;
}

//////////////////////////////////////////////////////////////////////////////
inline void ModelRowReference::setModel(QObject* m)
{
  model_ = m;
}

//////////////////////////////////////////////////////////////////////////////
inline int ModelRowReference::row() const
{
  return row_;
}

//////////////////////////////////////////////////////////////////////////////
inline void ModelRowReference::setRow(int r)
{
  row_ = r;
}

//////////////////////////////////////////////////////////////////////////////
inline QDataStream& operator<<(QDataStream& out, ModelRowReference const& p)
{
  QObject* model(p.model());

  out << QByteArray(reinterpret_cast<char const*>(&model), sizeof(model));
  out << p.row();

  return out;
}

//////////////////////////////////////////////////////////////////////////////
inline QDataStream& operator>>(QDataStream& in, ModelRowReference& r)
{
  QByteArray a;

  in >> a;

  r.setModel(*reinterpret_cast<QObject**>(a.data()));

  int row;

  in >> row;

  r.setRow(row);

  return in;
}

#endif // MODELROWREFERENCE_HPP

但它甚至无法编译:

modelrowreference.hpp:36:1: error: expected constructor, destructor, or type conversion before 'inline'

答案是,这可能做错了,还是我做错了什么?我知道我可以采用指针方式(注册 ModelRowReference*),但是谁来删除指针呢?可能会创建数千个 ModelRowReference 实例,而模型对象的生命周期会延长到程序结束。

我想要实现的是将对(模型、行)暴露给 QML 程序。如果我通过 QPair 执行此操作,则 QML 程序无法访问该对的成员。

我正在使用 Qt4.8。

最佳答案

您可能需要 #include <QMetaType> .

关于c++ - 在 QObject 子类中存储一对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10328946/

相关文章:

c++ - 使用键盘 : unintended side effects when pressing enter 将数据输入到 Qt GUI

c++ - 在 QTreeWidget 中插入元素时内存泄漏?

c++ - qt 4.5.0 与 Visual Studio 2008 的集成有什么问题?

c++ - 重载运算符删除,否则如何杀死一只猫?

android - 使用 Marmalade (C++) 开发移动应用程序

c++ - QWidget继承自自定义QWidget

c++ - Qt5将QImage转为OpenGL格式

c++ - SDL 游戏在加载二级时卡住

c++ - 等待条件的空循环(忙等待)

git - 将现有的 Qt Creator 项目添加到 Git