Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to refresh a QTableView after sorting the datasource object?
Qt 6.11 is out! See what's new in the release blog

How to refresh a QTableView after sorting the datasource object?

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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());
    }

    endInsertRows();

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

    // How to refresh the view?

    emit numRowsChanged();

    return true;
    }@

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You need to emit layoutChanged, see "this":http://qt-project.org/doc/qt-5/model-view-programming.html#resizable-models

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved