我有一个我的类列表(蓝色的),它有一个绿色类的列表,到目前为止我的 C++ 结构很好,我可以在 qml 中从 C++ 获取数据,反之亦然,但我不确定如何使 GUI 正常工作,我已经尝试使用嵌套的 ListView,但似乎我无法从内部 ListView 访问外部 ListView 模型。
我是 qml 的新手,昨天我找到了 TreeView,但对我来说,它看起来只有在你有一个表结构时才有用。是否有一些我不知道的 qml 可以帮助我解决这个问题?
我已经用嵌套的 ListView 尝试过这个,这个想法是内部 ListView 得到一个绿色类的对象作为模型。
ListView {
id: userView
anchors.fill: parent
model: myModel
delegate: Rectangle {
width: 900
height: 200
Column {
id: col
anchors.left: parent.left
anchors.right: parent.right
Item { height: 10 }
Text {
text: model.type + " " + model.name
}
Row {
spacing: 8
Button {
id: addLevel
width: 80
text: "Add Level"
enabled: setVisible
elevation: 1
backgroundColor: Theme.primaryColor
onClicked: {
myModel.insertLevel(index)
}
}
Button {
id: delTariff
width: 80
text: "Delete User"
enabled: setVisible
elevation: 1
backgroundColor: Theme.primaryColor
onClicked: {
myModel.removeTariff(index)
}
}
Button {
id: delLevel
width: 80
text: "Delete Level"
enabled: setVisible
elevation: 1
backgroundColor: Theme.primaryColor
onClicked: {
myModel.removeLevel(index, 0)
}
}
}
Text {
text: model.levels
}
Row {
spacing: 8
Repeater {
model: myModel.levelStructModel(userView.index)
Rectangle {
height: 30
width: 30
color: "blue"
}
}
}
}
}
我在添加或删除东西后也遇到程序崩溃的问题,我尝试在 myModel 的构造函数中添加 QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership)
,但没有任何运气.
最佳答案
经过一些搜索,Qt 论坛上一位好心人的帮助,他为我发布了这个。
SomeModel.h
class SomeModel : public QAbstractListModel
{
Q_OBJECT
public:
enum SomeModelRoles{
SOMETEXT = Qt::UserRole+1,
SUBMODEL
};
//....
// QAbstractItemModel interface
public:
int rowCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
QHash<int, QByteArray> roleNames() const;
protected:
QVector<SubModel*> m_models;
QHash<int, QByteArray> m_roles;
};
SomeModel.cpp
QVariant SomeModel::data(const QModelIndex &index, int role) const
{
if(!index.isValid())
return QVariant();
if(role == SOMETEXT)
return m_models.at(index.row())->someText();
else if(role == SUBMODEL)
return QVariant::fromValue(m_models.at(index.row()));
return QVariant();
}
子模型.h
class SubModel : public QAbstractListModel
{
//...
Q_PROPERTY(QString someText READ someText WRITE setSomeText NOTIFY someTextChanged)
public:
enum SubModelRoles{
COLOR = Qt::UserRole+1
};
//...
// QAbstractItemModel interface
public:
int rowCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
QHash<int, QByteArray> roleNames() const;
protected:
QHash<int, QByteArray> m_roles;
QVector<SomeItem*> m_items;
QString m_text;
};
子模型.cpp
QVariant SubModel::data(const QModelIndex &index, int role) const
{
if(!index.isValid())
return QVariant();
if(role == COLOR)
return m_items.at(index.row())->color();
return QVariant();
}
listModel.qml
ListView {
model: someModel // model from C++
spacing: 10
delegate: Component{
Rectangle {
width: 200
height: col.height
Column{
id: col
anchors.left: parent.left
anchors.top: parent.top
height: list.count*20 + header.height + buttonsRow.height
width: parent.width
Text{
id: header
width: parent.width
height: 40
text: someText
}
Row{
id: buttonsRow
anchors.horizontalCenter: parent.horizontalCenter
Button{
id: btnAdd
text: qsTr("Add")
}
Button{
id: btnDelete
text: qsTr("Delete")
}
}
ListView {
id: list
model: subModel
width: 0.7*parent.width
height: count*20
anchors.horizontalCenter: parent.horizontalCenter
delegate: Rectangle {
width: ListView.view.width
height: 20
color: itemColor
border.color: "gray"
border.width: 1
MouseArea{
anchors.fill: parent
onClicked: console.log(parent.color)
}
}
}
}
}
}
}
原来我的 C++ 代码并不像我想象的那么好,所以通过返回一个指向已知 QAbstractList 模型的指针,我可以在我的嵌套 ListView 中使用它。
来源:https://forum.qt.io/topic/68707/qt-nested-listview-or-can-i-use-treeview/3
关于c++ - Qt 嵌套 ListView 或者我可以使用 TreeView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37671430/