PDA

View Full Version : Merge 2 QSortFilterProxyModel



Damiano40
16th November 2015, 16:51
Hi,

i have a QSqlQueryModel with about 5000 records with 4 columns.

I want to filter the contents of column 1 of the source model respect a fixedstring specified in a QLineEdit.



QString searchText = ui->searchBar->text().trimmed();
MySortFilterProxyModel *proxy = new MySortFilterProxyModel(0);
proxy->setSourceModel(model);
proxy->setFilterKeyColumn(1);
proxy->setFilterCaseSensitivity(Qt::CaseInsensitive);
proxy->setFilterFixedString(searchText);


The task that i can't figure out is:
I want to sort results respect two categories:
- Results that start with searchText
- All other results

Is it possible to merge two QSortFilterProxyModel?

Please help. Thanks in advance.

anda_skoa
17th November 2015, 08:48
While you can stack proxy models, why not implement your proxy's lessThan() method so that it takes the search criteria into account?

Cheers,
_

Damiano40
17th November 2015, 09:40
I tried implementing my lessThan() function and it works.

The problem is the slowness of the function.

My situation is (supposed i want to search 'E' caseinsensitive):

row - string

0001 - row1E
0002 - row2 E row2
0003 - Erow3
0004 - row4E
0005 - E row5

and i want to sort in:

row - string

0003 - Erow3
0005 - E row5
0001 - row1E
0002 - row2 E row2
0004 - row4E

I just need to put on the top the row that start with E without change any other order;

Is there a way to speed up this process?

anda_skoa
17th November 2015, 10:33
You could use two proxies, one only including the rows with E and one excluding the rows with E and then creating a new model that just operates on the two proxies.

Alternatively write a model that uses QSqlQuery for the database access and keeps its own mapping of model rows to DB rows.
It could the just move the matching rows to the front of its internal list.

Cheers,
_

Damiano40
17th November 2015, 14:59
Can you explain me something more about creating a new model that just operates on the two proxies.

i have done what you say with


MySortFilterProxyModel *proxy = new MySortFilterProxyModel(0);
proxy->setSourceModel(model);
proxy->setFilterKeyColumn(1);
proxy->setFilterCaseSensitivity(Qt::CaseInsensitive);
proxy->setFilterFixedString(searchText);

MySortFilterProxyModel *proxy1 = new MySortFilterProxyModel(1);
proxy1->setSourceModel(model);
proxy1->setFilterKeyColumn(1);
proxy1->setFilterCaseSensitivity(Qt::CaseInsensitive);
proxy1->setFilterFixedString(searchText);


but i can't figure out how to "append" proxy1 to proxy...

thanks

anda_skoa
17th November 2015, 15:50
You create a new model by deriving from QAbstractTableModel and then implementing rowCount(), columnCount() and data().

columnCount() is the columnCount() of either source model.
rowCount() is obviously the sum of the source models.
data() then needs to check which row it gets, create an index for the appropriate sub model with the same column and call data with the new index and the same role.

Cheers,
_