How to refresh a QTableView after sorting the datasource object?

  • I have a custom QTableView and QAbstractTableModel. The data is backed by a std::deque and must be sorted by a particular field. Since the actual data source has to be sorted, instead of using a proxy model I am using std::sort(). Inside my custom implementation of QAbstractTableModel::insertRows I must recall std::sort() to ensure that the newly inserted row is sorted. After calling std::sort() on the datasource, what is the proper call to make inside insertRows to refresh the view?

    @bool MomentValuePairTableModel::insertRows(int32_t position, int32_t rows, const QModelIndex &)
    beginInsertRows(QModelIndex(), position, position + rows - 1);

    for (int32_t row = 0; row < rows; ++row)
    std::deque<MomentValuePair>::iterator it = momentValuePairs.begin() + position;
    momentValuePairs.insert(it, MomentValuePair());


    // Sort the datasource.
    std::sort(momentValuePairs.begin(), momentValuePairs.end());

    // How to refresh the view?

    emit numRowsChanged();

    return true;

  • Lifetime Qt Champion

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.