Qt World Summit: Submit your Presentation

Sorting list from QSelectionModel::selectedIndexes() in "display order"?

  • Is there a simple and direct way to sort the output from selectionModel ()->selectedIndexes() on a certain item view derived from QAbstractItemView in the order the data of the indexes will actually appear in a display? I mean, like in top-down, then left-to-right order in the case of a table view. This is of course nearly a simple sort on row() and column() of the indexes, but not quite - the fact that there are multiple levels defined by a parent-child relationship complicates matters. And QModelIndex::operator<() (which a sort routine will typically use) does not take this into account.

    It also seems like the list often has a "correct" data order, but this is not always the case - it seems like it somehow depends on how exactly the selection was made...

  • QModelIndex defines "operator<() ":/doc/qt-4.8/qmodelindex.html#operator-lt, so you can use qSort() on a QModelIndexList (which is just a QList<QModelIndex>).

    inline bool operator<(const QModelIndex &other) const
    if (r < other.r) return true;
    if (r == other.r) {
    if (c < other.c) return true;
    if (c == other.c) {
    if (p < other.p) return true;
    if (p == other.p) return m < other.m;
    return false; }

    This works fine for tables, bot not that good for trees.

  • Any solutions for trees?

    I need to sort selected indexes from children to parents..

Log in to reply