您可以從主QAbstractListModel返回QVariantList,然後這可以被指定爲模型內部的ListView您在委託有。我添加了一個以內部模型爲例的非常簡單的單行模型的小例子。
C++模型類:
class TestModel : public QAbstractListModel
{
public:
enum EventRoles {
StringRole = Qt::UserRole + 1
};
TestModel()
{
m_roles[ StringRole] = "stringList";
setRoleNames(m_roles);
}
int rowCount(const QModelIndex & = QModelIndex()) const
{
return 1;
}
QVariant data(const QModelIndex &index, int role) const
{
if(role == StringRole)
{
QVariantList list;
list.append("string1");
list.append("string2");
return list;
}
}
QHash<int, QByteArray> m_roles;
};
現在,你可以把這個模型QML,並使用它像這樣:
ListView {
anchors.fill: parent
model: theModel //this is your main model
delegate:
Rectangle {
height: 100
width: 100
color: "red"
ListView {
anchors.fill: parent
model: stringList //the internal QVariantList
delegate: Rectangle {
width: 50
height: 50
color: "green"
border.color: "black"
Text {
text: modelData //role to get data from internal model
}
}
}
}
}
感謝。它也證明你可以將模型作爲'QVariant'公開爲:'QVariant :: fromValue(myModelInstancePointer);' –