如何将自定义对象定义为 QAbstractListModel 中的角色?

How to define a custom object as a role in QAbstractListModel?

我的问题是,如何将自定义对象指定为派生自 QAbstractListModel 的模型中的角色,以便在 ListView 中可视化它时,我可以访问其成员变量。这里有一个例子是一些简单的代码示例:

这是我的 class 代表我的自定义对象:

class MyCustomObject {
  public:
    MyCustomObject(Qstring name, Qstring type);
    QString getName();
    QString getType();

  private:
    QString name;
    QString type;
};

这就是我的 MyModel 派生自 QAbsractListModel:

的重写 data() 函数现在的样子(但它不工作)
QVariant MyModel::data(const QModelIndex &index, int role) const {
    if (index.row() < 0 || index.row() > m_atoms.count()) {
    //if (!index.isValid()) {
        return QVariant();
    }

    const MyData &data = m_data[index.row()];
    if(role == SomeRole) {
        return data.someString()
    }
    else if (role == MyCustomRole) {
        return data.myCustomObject; // How can I do this?
    }

    return QVariant();
}

这里我指定角色名在MyModel:

QHash<int, QByteArray> AtomModel::roleNames() const {
    QHash<int, QByteArray> roles;
    roles[SomeRole] = "someRole";
    roles[MyCustomRole] = "myCustomRole";

    return roles;
}

这就是我的 ListView 在 QML 代码中的样子,并举例说明我想如何访问委托中的 MyCustomObject 成员变量:

ListView {
        width: 400
        height: 400

        model: myModel
        delegate: Text {
            text: "Type: " + myCustomRole.getType() + ", Name: " + myCustomRole.getName() + ", some string: " someRole

        }
    }

EDIT1: => 修复所需的复制构造函数

当我在 MyCustomObject 下添加 Q_DECLARE_METATYPE 时,我收到以下错误:

call to implicitly-deleted copy constructor of `MyCustomObject`
in instantiation of member function 'QtMetaTypePrivate::QMetaTypeFunctionHelper<MyCustomObject, true>::Construct' requested here
in instantiation of function template specialization 'qRegisterNormalizedMetaType<MyCustomObject>' requested here QtMetaTypePrivate::QMetaTypeFunctionHelper<T>::Construct,
    return qRegisterNormalizedMetaType<T>(normalizedTypeName, dummy, defined);
in instantiation of function template specialization 'qRegisterMetaType<MyCustomObject>' requested here
Q_DECLARE_METATYPE(MyCustomObject)
expanded from macro 'Q_DECLARE_METATYPE'
#define Q_DECLARE_METATYPE(TYPE) Q_DECLARE_METATYPE_IMPL(TYPE)
expanded from macro 'Q_DECLARE_METATYPE_IMPL' 
      const int newId = qRegisterMetaType< TYPE >(#TYPE,
copy constructor of 'MyCustomObject' is implicitly deleted because base class 'QObject' has a deleted copy constructor
class MyCustomObject : public QObject
'QObject' has been explicitly marked deleted here Q_DISABLE_COPY(QObject)
expanded from macro 'Q_DISABLE_COPY'
       Class(const Class &) Q_DECL_EQ_DELETE;\

编辑 2:

所以我添加了@Evgeny 建议的所有必要功能。我的代码现在编译没有错误,但是我在 运行 时间收到一个 qml 错误说: TypeError: Property 'getType' of object QVariant(MyCustomObject) is not a function

我在 getType() 方法前面添加了 Q_INVOKABLE,并且我还从 public QObject 派生了 MyCustomObject class。我在 MyCustomObject 头文件的底部添加了 Q_DECLARE_METATYPE。在 MyCustomObject 的构造函数中我调用了 qRegisterMetaType<MyCustomObject>("MyCustomObject") 并且在我的 main 中我注册了 class 也像这样 qmlRegisterType<MyCustomObject>("com.test.mycustomobject", 1, 0, "MyCustomObject")

这就是 MyCustomObject class 现在的样子:

class MyCustomObject : public QObject {
  public:
    MyCustomObject();
    MyCustomObject(Qstring name, Qstring type);
    MyCustomObject(const MyCustomObject& obj);
    ~MyCustomObject();
    Q_INVOKABLE QString getName();
    Q_INVOKABLE QString getType();

  private:
    QString name;
    QString type;
};
Q_DECLARE_METATYPE(MyCustomObject)

这就是我的 MyModel 派生自 QAbsractListModel:

的重写 data() 函数现在的样子
QVariant MyModel::data(const QModelIndex &index, int role) const {
        if (index.row() < 0 || index.row() > m_atoms.count()) {
        //if (!index.isValid()) {
            return QVariant();
        }

        const MyData &data = m_data[index.row()];
        if(role == SomeRole) {
            return data.someString()
        }
        else if (role == MyCustomRole) {
            QVariant var; // this is the part, which has changed
            var.setValue(data.myCustomObject);
            return var;
        }

        return QVariant();
    }

我原贴的其他功能都是一样的

首先,您需要为 Qt 元类型系统声明您的自定义对象。你应该为此使用 Q_DECLARE_METATYPE macro for this. Also you may need to use qRegisterMetaType function. Then you should register your object to use it with QML. You should use qmlRegisterType 函数。

还要确保对对象方法使用 Q_INVOKABLE