Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Can';t访问ListView中的QAbstractListModel数据_C++_Qt_Qml_Qabstractlistmodel - Fatal编程技术网

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
    }
}
  • 详细资料: