Important: Please read the Qt Code of Conduct -

Getting very confused trying to get all children of a selected thingy in a QTreeView

  • I have a QTreeView. It shows a hierarchy of files in directories.

    When the user selects something that has children (i.e. a directory) I want to automatically select all the children. I get the index of the selected directory like this:

    @ QModelIndexList selectedIndices = ui->projectExplorerTreeView->selectionModel()->selectedIndexes();@

    That actually gives me four indices (supposing only one line in the QTreeView is selected). The first column (i.e. column number zero) is the one that has children, judging by the picture in my QTreeView window.

    I yomp through all the selected indices like this (one of them is my little directory picture with children, the other indices are other columns

    @ for (int i = 0; i<selectedIndices.size(); ++i)
    if ( == 0) // this one is the one that might have children
    // Get index value of all children

    I was expecting to be able to interrogate any given index to ask it for its children. I see the function @QModelIndex QModelIndex::child ( int row, int column ) const@
    which says it "Returns the child of the model index that is stored in the given row and column."

    But if I knew which row and column in the model contained a child, I wouldn't need this function. I must be misunderstanding this. Is this how I'm meant to get the children of a given index?

Log in to reply