Solved [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:
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 theQtableView
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. TheQtableview
in default order would look like this:However, the user can click either the
Name
,Events
orDate
column headers, which should reorganize the rows corresponding to that column's values after they've been sorted. For instance, If the user clicks theDate
column header I want to to sort all rows corresponding to theDate
column after it has been sorted with the most recent date first. As another example, if the user clicks theName
column header, I sort the rows corresponding to theName
column after it has been sorted in alphabetical order. For instance here's what theQtableview
should look like after I've sorted byName
: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? IsQTableView
overkill, should I useQTablewidget
instead? Or isQTableView
appropriate? -
Hi,
Looks like you should take a look at QSortFilterProxyModel.