4

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

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

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

  private:
    QString name;
    QString type;
};

这就是我派生的被覆盖data()函数现在的样子(但它不起作用):MyModelQAbsractListModel

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:=> 修复所需的复制构造函数

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

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

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

这是MyCustomObject现在类的样子:

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)

这就是我派生的被覆盖data()函数现在的样子:MyModelQAbsractListModel

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();
    }

我最初发布的所有其他功能都是相同的。

4

1 回答 1

2

首先,您需要为 Qt 元类型系统声明您的自定义对象。您应该Q_DECLARE_METATYPE为此使用宏。此外,您可能需要使用qRegisterMetaType功能。然后您应该注册您的对象以将其与 QML 一起使用。您应该qmlRegisterType为此使用功能。

还要确保你使用Q_INVOKABLE你的对象方法。

于 2016-03-08T09:07:28.357 回答