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.
RaisinBread22 last edited by RaisinBread22
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:
I want to create a
QtableViewthat 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
QtableViewby 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
Qtableviewin default order would look like this:
However, the user can click either the
Datecolumn headers, which should reorganize the rows corresponding to that column's values after they've been sorted. For instance, If the user clicks the
Datecolumn header I want to to sort all rows corresponding to the
Datecolumn after it has been sorted with the most recent date first. As another example, if the user clicks the
Namecolumn header, I sort the rows corresponding to the
Namecolumn after it has been sorted in alphabetical order. For instance here's what the
Qtableviewshould look like after I've sorted by
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
QTableViewevery time the user clicks one of the column headers? Is
QTableViewoverkill, should I use
QTablewidgetinstead? Or is
Looks like you should take a look at QSortFilterProxyModel.