Qt嵌套ListView或者我可以使用TreeView吗?

8
我正在开展一个项目,希望有如下图所示的GUI界面: 树状图形式的GUI 我的类列表(蓝色区域)包含一个绿色类的列表。目前,我的C++结构很好,并且我可以在qml和C++之间获取数据,但是我不确定如何使GUI正常工作。我尝试使用嵌套的ListViews,但似乎无法从内部ListView访问外部ListView模型。
我对qml还比较新,昨天我发现了TreeView,但在我看来,如果你有一个表格结构,它似乎只能用于这个。是否有我不知道的qml可以帮助我完成这个?
我已尝试使用嵌套的ListViews,想法是内部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),但没有成功。

1个回答

9

经过一番搜索和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();
}

SubModel.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;
};

SubModel.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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接