Qt World Summit: Register Today!

QModelIndex: Get for same column in parent?

  • Hello,

    I'm working with the sample code at:

    Given a QModelIndex pointing at a certain item, how do I get a QModelIndex for the item's parent that points to the same column?

    Just calling someIndex.parent() will always return a QModelIndex with column == 0.

    In this sample case, since each row has the same number of columns, so we know for sure that every parent (other than the invisible root) certainly has such a column.

    What is the correct method?

    Thank you

  • You could do something like this:

    QModelIndex parent = someIndex.parent();
    QModelIndex parentColumn = someIndex.model().index(parent.parent(), parent.row(), someIndex.column());

Log in to reply