Please nominate your Qt Champions for 2021!

How to get a child of rootIndex() in QTreeView ?

  • I want to get all top level items in my tree view to make a loop. I do next:
    @QModelIndex root(rootIndex());

    int row = 0;
    QModelIndex sessionIndex = root.child(row,SessionsTreeModel::eName);
    while (sessionIndex.isValid()) {
        sessionIndex = sessionIndex.sibling(row,SessionsTreeModel::eName);
        int id = m_model->data(sessionIndex,Qt::UserRole).toInt();
        if (sessionID == id) {

    But my sessionIndex always invalid. I find next in Qt Assistant : bq. QModelIndex QModelIndex::child ( int row, int column ) const

    Returns the child of the model index that is stored in the given row and column.

    Note: This function does not work for an invalid model index which is often used as the root index. bq.

    So how can I get child of rootIndex ?

  • @
    m_model->index(row, column, rootIndex());
    any good?

Log in to reply