C++ Can';t访问ListView中的QAbstractListModel数据
我有C++ Can';t访问ListView中的QAbstractListModel数据,c++,qt,qml,qabstractlistmodel,C++,Qt,Qml,Qabstractlistmodel,我有Foo类,它是从QAbstractListModel派生的。以及我在qml中注册并创建的Bar类。Bar类保存作为属性公开的Foo对象 class Foo : public QAbstractListModel { Q_OBJECT public: explicit Foo(QObject *parent = nullptr) : QAbstractListModel(parent) { mList.append("test1"); mList
Foo
类,它是从QAbstractListModel
派生的。以及我在qml中注册并创建的Bar
类。Bar类保存作为属性公开的Foo
对象
class Foo : public QAbstractListModel
{
Q_OBJECT
public:
explicit Foo(QObject *parent = nullptr) : QAbstractListModel(parent) {
mList.append("test1");
mList.append("test2");
mList.append("test3");
}
virtual int rowCount(const QModelIndex &parent) const Q_DECL_OVERRIDE {
return mList.count();
}
virtual QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE {
return mList.at(index.row());
}
private:
QStringList mList;
};
class Bar : public QQuickItem
{
Q_OBJECT
Q_PROPERTY(Foo* foo READ foo NOTIFY fooChanged)
public:
explicit Bar(QQuickItem *parent = nullptr)
: QQuickItem(parent) {
mFoo = new Foo(this);
}
Foo *foo() const { return mFoo; }
signals:
void fooChanged(Foo *foo);
private:
Foo *mFoo;
};
寄存器Bar
类型:
qmlRegisterType<Bar>("Custom", 1, 0, "Bar");
我创建ListView并分配模型Foo
。
预期结果是看到用“test1”、“test2”、“test3”填充的代理文本,但我得到以下结果:
ReferenceError: modelData is not defined
ReferenceError: modelData is not defined
ReferenceError: modelData is not defined
QML引擎是正确的,
modelData
未定义。在代理中,model
未定义modelData
另外,由于在QAbstractListModel
中您没有定义自己的角色,因此可以使用默认角色<代码>显示是默认角色,您可以使用它。因此,您的列表视图应该如下所示:
ListView {
id: someList
model: bar.foo
delegate: Text {
text: model.display
}
}
- 详细资料:
QML引擎是正确的,modelData
未定义。在代理中,model
未定义modelData
另外,由于在QAbstractListModel
中您没有定义自己的角色,因此可以使用默认角色<代码>显示
是默认角色,您可以使用它。因此,您的列表视图应该如下所示:
ListView {
id: someList
model: bar.foo
delegate: Text {
text: model.display
}
}
- 详细资料: