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
を割り当てます。 modelData
が定義されていない、
ReferenceError: modelData is not defined
ReferenceError: modelData is not defined
ReferenceError: modelData is not defined