QML Treeview:如何获取孩子的 QModelIndex

QML Treeview: How to get QModelIndex of childs

我有一个带有 QStandardItemModel 的 QML TreeView 并使用 ItemSelectionModel 来管理 selection。 ItemSelectionModel 需要一个 QModelIndex 作为它的 select 函数。在我看来如何获得children的QModelIndex

树看起来像这样:

我想 select task2 当我点击它时(我可以在委托中有一个 MouseArea)(以便 TreeView 突出显示它),为了做到这一点,我必须调用 ItemSelectionModel.select 与任务 2 的 QModelIndex。但我不 不知道如何获取task2的QModelIndex。

QStandardItemModel 派生自 QAbstractItemModel 因此提供了一个索引函数:

 virtual QModelIndex    index(int row, int column, const QModelIndex & parent = QModelIndex()) const

但要使用此函数,我需要知道 parent 的索引。我怎样才能从视图中得到它?

要获得child你必须先有parent,所以在你的方案中你必须获得"file1"和为此你必须得到他的parent,而这个parent是TreeViewrootIndex,所以顺序是:rootIndex -> file1 -> task1.

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QStandardItemModel>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    QStandardItemModel model;

    QStandardItem *item1 = new QStandardItem("file1");
    item1->appendRows({new QStandardItem("task1"), new QStandardItem("task2")});

    QStandardItem *item2 = new QStandardItem("file2");
    item2->appendRows({new QStandardItem("task1")});

    model.appendRow(item1);
    model.appendRow(item2);

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("tree_model", &model);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}

main.qml

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQml.Models 2.11

Window {
    visible: true
    width: 640
    height: 480
    TreeView {
        id: treeView
        anchors.fill: parent
        model: tree_model
        selectionMode: SelectionMode.MultiSelection
        selection: ItemSelectionModel {
            id: ism
            model: tree_model
        }
        TableViewColumn {
            title: "Name"
            role: "display"
            width: 300
        }
        Component.onCompleted: {
            expandAll()

            var ix1 = tree_model.index(0, 0, treeView.rootIndex)
            var ix = tree_model.index(0, 0, ix1)
            ism.select(ix, ItemSelectionModel.Select)
        }
    }

    // https://forum.qt.io/topic/75395/qml-treeview-expand-method-not-working
    function expandAll() {
        for(var i=0; i < tree_model.rowCount(); i++) {
            var index = tree_model.index(i,0)
            if(!treeView.isExpanded(index)) {
                treeView.expand(index)
            }
        }
    }
}

更新:

要获取按下的项目的索引,您必须使用 styleData.index:

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQml.Models 2.11

Window {
    visible: true
    width: 640
    height: 480
    TreeView {
        id: treeView
        anchors.fill: parent
        model: tree_model
        selectionMode: SelectionMode.MultiSelection
        selection: ItemSelectionModel {
            id: ism
            model: tree_model
        }
        TableViewColumn {
            title: "Name"
            role: "display"
            width: 300
        }
        itemDelegate: Item {
            Text {
                anchors.verticalCenter: parent.verticalCenter
                color: styleData.textColor
                elide: styleData.elideMode
                text: styleData.value

            }
            MouseArea{
                anchors.fill: parent
                onClicked: {
                    var ix = tree_model.index(0, 0, styleData.index)
                    ism.select(ix, ItemSelectionModel.Select)
                }
            }
        }
    }
}