Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ qmltreeview:如何获取childs的QModelIndex_C++_Qt_Qml_Qt5_Qstandarditemmodel - Fatal编程技术网

C++ qmltreeview:如何获取childs的QModelIndex

C++ qmltreeview:如何获取childs的QModelIndex,c++,qt,qml,qt5,qstandarditemmodel,C++,Qt,Qml,Qt5,Qstandarditemmodel,我有一个带有QStandardItemModel的QMLTreeView,并使用ItemSelectionModel来管理选择。ItemSelectionModel需要一个QModelIndex作为其select功能。如何获取视图中儿童的QModelIndex 这棵树看起来像这样: 文件1 任务1 任务2 文件2 任务1 我想在单击task2时选择它(我可以在代理中有一个MouseArea)(以便树状视图突出显示它),为此,我必须调用ItemSelectionModel。使用task

我有一个带有
QStandardItemModel
的QML
TreeView
,并使用
ItemSelectionModel
来管理选择。
ItemSelectionModel
需要一个
QModelIndex
作为其
select
功能。如何获取视图中儿童的
QModelIndex

这棵树看起来像这样:

  • 文件1
    • 任务1
    • 任务2
  • 文件2
    • 任务1
我想在单击task2时选择它(我可以在代理中有一个MouseArea)(以便树状视图突出显示它),为此,我必须调用
ItemSelectionModel。使用task2的
QModelIndex
选择
。但我不知道
我不知道如何获得task2的QModelIndex

QStandardItemModel
源自
qabstracttemmodel
,因此提供了一个索引函数:

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

但是要使用这个函数,我需要知道父对象的索引。我怎样才能从视野中看到它

要获得子级,您必须首先拥有父级,因此在您的方案中,您必须获得“file1”,并且为此您必须获得其父级,而此父级是
树视图的
rootIndex
,因此顺序是:
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();
}

你说的孩子是什么意思?也就是说,谁是父母?我应该说得更具体些。选择应通过单击代理的鼠标区域进行。问题是要在task1的MouseArea中找到正确的ix1(这里是file1)。
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)
            }
        }
    }
}
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)
                }
            }
        }
    }
}