简体   繁体   English

将对存储在QObject子类中

[英]storing a pair in a QObject subclass

My question refers to question Subclass of QObject, qRegisterMetaType, and the private copy constructor . 我的问题涉及QObject的问题子类,qRegisterMetaType和私有副本构造函数 According to it, it should be possible to register a subclass of QObject as a meta-type, that a QVariant can store. 据此,应该有可能将QObject的子类注册为QVariant可以存储的元类型。 I have this code: 我有以下代码:

#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

but it doesn't even compile: 但它甚至不能编译:

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

Is the answer, that this can be done wrong, or am I doing something wrong? 答案是,这可能做错了,还是我做错了? I know I could have gone the pointer way (registering ModelRowReference*), but who then is going to delete the pointer? 我知道我本可以使用指针方式(注册ModelRowReference *),但是谁将删除该指针呢? Thousands of instances of ModelRowReference might be created, while the model object lifetime extends until the end of the program. 可能会创建成千上万的ModelRowReference实例,而模型对象的生存期会延长到程序结束。

What I am trying to achieve is exposing the pair (model, row) to a QML program. 我要实现的目标是将该对(模型,行)暴露给QML程序。 If I do it via QPair, the QML program is unable to access the members of the pair. 如果我通过QPair进行操作,则QML程序无法访问该对的成员。

I am using Qt4.8. 我正在使用Qt4.8。

您可能需要#include <QMetaType>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM