0
我有Foo
類從QAbstractListModel
派生。和我在qml註冊並創建的類Bar
。酒吧類持有Foo
對象作爲財產暴露。無法訪問ListView中的QAbstractListModel數據
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");
QML:
import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Controls 2.0
import Custom 1.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
ListView {
id: someList
model: bar.foo
delegate: Text {
text: modelData
}
}
Bar {
id: bar
}
}
創建ListView和分配模型Foo
。 預期的結果是看委託的文字充滿了「測試1」,「測試2」,「TEST3」,但我得到這個:
ReferenceError: modelData is not defined
ReferenceError: modelData is not defined
ReferenceError: modelData is not defined