Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[Pyside2] Representing data from databse in QListView and allowing rows to be sorted by columns.



  • I have a simple Sqlite database table where each row contains 3 values: a name, date, and image file path. Each time a particular event(that pertains to my application) is performed by the user, a new row is added with the users' name, the date of the event, and a file path to a screenshot of the event. A table might look somewhat like this:

    alt text

    I want to create a QtableView that uses a portion the data in the database table: the names and dates, in addition to the number of times each user is in the database(equal to the # of events they've performed). I want the QtableView by default to be sorted in descending order by the number of times each user has performed an event, with their most recent event date. The Qtableview in default order would look like this:

    alt text

    However, the user can click either the Name, Events or Date column headers, which should reorganize the rows corresponding to that column's values after they've been sorted. For instance, If the user clicks the Date column header I want to to sort all rows corresponding to the Date column after it has been sorted with the most recent date first. As another example, if the user clicks the Name column header, I sort the rows corresponding to the Name column after it has been sorted in alphabetical order. For instance here's what the Qtableview should look like after I've sorted by Name:

    alt text

    As I see it there are two main times when I'll need to update the QTableView: when the application first starts up, and each time a new row is added to the database(the user performs an event). Upon the startup of the Qt application, I'll be able to call a function I've defined in the database class that tells me all of the user's names, the number of events they've performed and the most recent event date. Each time a new row is added to the database after a user
    performs an event, I send a signal to the main Qt event loop from the database class, so I can update the view with that new row.

    My question: What type of model should I use to store this data? What functions might I need to overload to reorganize the QTableView every time the user clicks one of the column headers? Is QTableView overkill, should I use QTablewidget instead? Or is QTableView appropriate?


  • Lifetime Qt Champion

    Hi,

    Looks like you should take a look at QSortFilterProxyModel.


Log in to reply