PDA

View Full Version : Search in tableView with 2 arguments



alexandrius
14th September 2013, 13:34
Hello,
I want to make a search with 2 arguments in tableView with regExp,
here's the code:

QString search = QInputDialog::getText(this,"Search","Enter search keyword",QLineEdit::Normal);
QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
proxyModel->setSourceModel(model);
proxyModel->setFilterRegExp(QRegExp(search, Qt::CaseInsensitive, QRegExp::FixedString));
proxyModel->setFilterKeyColumn(-1);
ui->tableView->setModel(proxyModel);
ui->tableView->show();
I want to search for Name and Surname matching exactly the correct row. How do i do that?

wysota
15th September 2013, 00:31
Subclass the proxy model and reimplement filterAcceptsRow().

alexandrius
16th September 2013, 14:23
@wysota
I'm sorry can you show an example

wysota
16th September 2013, 15:47
@wysota
I'm sorry can you show an example

Docs for QSortFilterProxyModel contain an example of a custom filterAcceptsRow() method.

alexandrius
16th September 2013, 21:11
Docs for QSortFilterProxyModel contain an example of a custom filterAcceptsRow() method.
Never understood anything from documentation. Please give me a practical example if you can

wysota
16th September 2013, 23:57
Never understood anything from documentation.
Apparently there is something wrong with your reading skills.


Please give me a practical example if you can


bool MySortFilterProxyModel::filterAcceptsRow(int sourceRow,
const QModelIndex &sourceParent) const
{
QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
QModelIndex index1 = sourceModel()->index(sourceRow, 1, sourceParent);
QModelIndex index2 = sourceModel()->index(sourceRow, 2, sourceParent);

return (sourceModel()->data(index0).toString().contains(filterRegExp())
|| sourceModel()->data(index1).toString().contains(filterRegExp()))
&& dateInRange(sourceModel()->data(index2).toDate());
}

alexandrius
19th September 2013, 09:55
I was too lazy for creating new class.
So i used the old method for filtering.
Then filtered already filtered model, that's it problem solved

wysota
19th September 2013, 10:06
You're probably right. Writing a subclass takes 10-15 minutes. "Solving" the problem "your way" -- 6 days. Good job.

alexandrius
22nd September 2013, 19:32
You're probably right. Writing a subclass takes 10-15 minutes. "Solving" the problem "your way" -- 6 days. Good job.
First of all it takes maximum of 2 minutes. 2nd It doesn't mean i was 6 days fixing the problem, I'm rarely here. 3rd this way solves the problem very well. 4th you are a d1ck.
I asked for life example not the documentation but you started joking about my reading skills, if you can't give an example do not do it and do not write that i can't read English. Not everyone knows Qt as well as you!

you are a big pr1ck seriously.

Sincerely Alex. I wish your d1ck will wither out

wysota
22nd September 2013, 19:57
I asked for life example not the documentation
What's wrong with the example from the documentation? It does almost exactly what you wanted, except that in addition it checks the date.

Thank you for you good wishes, I wish you all the best too.