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 permanently sort a QStandardItemModel?
QtWS25 Last Chance

How to permanently sort a QStandardItemModel?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qstandarditemmoqtableviewc++sorting
3 Posts 3 Posters 1.8k Views
  • 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.
  • S Offline
    S Offline
    stor314
    wrote on 9 Sept 2017, 20:39 last edited by
    #1

    I have a program where I am trying to implement sorting on a qstandarditemmodel that is displayed in a table view. However, the method I am using doesn't seem to actually sort the model itself, but only the view. I need it to be able to sort the source model because I save the data to a .csv file using a delegate that passes the items from the model into an object of a class, and if the view is the only thing that is sorted it causes data loss due to the positions of the items in the view being changed but not in the model itself.

    This is the code I use in the mainwidget constructor to connect the headerview clicked signal to a method that sorts the model:

    currentStudentsModel = new QStandardItemModel(this);
    
    ui->currentTableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->currentTableView->setModel(currentStudentsModel);
    
    ui->currentTableView->setItemDelegate(currentStudentsDelegate);
    currentTableHeader = ui->currentTableView->horizontalHeader();
    connect(currentTableHeader, SIGNAL(sectionClicked(int)), this, SLOT(on_sectionClicked(int)));
    

    Here is on_sectionClicked():

    void mainWidget::on_sectionClicked(int index)
    {
       currentStudentsModel->sort(index,Qt::AscendingOrder);
    }
    

    As I previously stated, this appears to only sort the items in the view as when I try to output all of the records stored in the model it has not changed from when they were initially entered. How do I get the model to be sorted itself and that order to be saved?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 11 Sept 2017, 05:16 last edited by
      #2

      Use a QAbstractItemModel - that one allows you to take full control over the underlying data (you can store it in a QVector, for example - then sorting is very easy).

      The disadvantage here is that to make the abstract model work you'll have to reimplement some more functions in your subclass.

      (Z(:^

      1 Reply Last reply
      1
      • V Offline
        V Offline
        VRonin
        wrote on 11 Sept 2017, 09:32 last edited by
        #3

        Put a QSortFilterProxy model in-between. then save from the proxy instead of the main model.

        P.S.
        connect(currentTableHeader, SIGNAL(sectionClicked(int)), this, SLOT(on_sectionClicked(int))); and void mainWidget::on_sectionClicked(int index) are useless. this is the default behaviour of QTableView, you don't have to implement it manually

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        3

        1/3

        9 Sept 2017, 20:39

        • Login

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