簡體   English   中英

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

[英]Qt nested ListView or can I use TreeView

我正在做一個項目,我想要一個這樣的 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)添加QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership) ,但沒有任何運氣。

所以經過一些搜索,並得到 Qt 論壇上一位好人的幫助,他為我發布了這個。

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

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();
}

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

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();
}

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM