PDA

View Full Version : Adding a new row with a QSortFilterProxyModel



PinTxO
21st June 2017, 09:57
I have a QTableView whose data model is a class derived from QSortFilterProxyModel that I have created myself. In this class we reimplemented the method filterAcceptsRow to be able to filter the data of the table according to several criteria. I can also sort the table by any of the fields in it.

The problem arises when the user clicks the "Create" button, which creates a new empty row in the table. If I have a filter applied, the empty row does not appear because it does not meet this filter, which I do not want to happen because the user has to start to edit their data obligatorily.

Also, when I have the table ordered by a field, when I add the empty row, it automatically positions itself in the position that it plays according to the sorting criteria, which is not desirable because I want it always to be in the first position.

Any idea how you can fix this problem?

Santosh Reddy
21st June 2017, 12:12
A simple way would be to put some data in Qt::UserRole, in the newly created row items (or row items subjected to editing), then avoid filtering them (in filterAcceptsRow implementation) and fix the sorting position (in lessThan implementation), either top/bottom (or as you like).

d_stranz
21st June 2017, 17:39
Any idea how you can fix this problem?

An alternative is to reverse the process: let the user enter the information into a form (not the table), then create a new row with the information when the user clicks the Add button. This also has the advantages of letting the user easily cancel the operation and of letting you validate the information before it gets to the model. If you add the row first and then edit directly in the table, it leads to the problems you are having and also makes it awkward for the user to back out.