QTableview Search
-
Hello everyone, i am doing a search engine for a QTableview, and this is what i have until now:
void Inventory::on_Search_clicked(){ QModelIndexList matchList = ui->tableView->model()->match(ui->tableView->model()->index(0,0), Qt::EditRole, ui->lineEdit->text(), -1, Qt::MatchFlags(Qt::MatchContains|Qt::MatchWrap)); if(matchList.count()>=1){ ui->tableView->setCurrentIndex(matchList.first()); ui->tableView->scrollTo(matchList.first()); } QSound::play("/home/adan/Groostore/Resources/Sounds/Update.wav"); }
It works as required, but i want to add another functionality:
This code search for specific matches with a QLineedit and it selects and scrolls to the first value of the QModelindexList, but, if i click a second time searching the same text on the QLineedit, i would like the selection to scroll to the 2 item on the QModelindexList, i have tried many things with no luck, i hope you can help on this, Thanks!
-
Well, at the end i figured it out:
void Registry::on_Search_clicked(){ from++; QString text = ui->lineEdit_2->text(); if(text.isEmpty()) return; QModelIndexList indexes = ui->tableView->model()->match(ui->tableView->model()->index(0,0),Qt::EditRole,text,-1, Qt::MatchFlags(Qt::MatchContains|Qt::MatchWrap)); if(indexes.length() > from){ QModelIndex ix = indexes.at(from); ui->tableView->setCurrentIndex(ix); ui->tableView->scrollTo(ix); } QSound::play("/home/adan/Groostore/Resources/Sounds/Update.wav"); }
The int variable "from" is declared as public on the header.
Thanks!
-
Hi,
Keep the current search as well as the index of the item in it that you are showing. Then have a next button that will increment that index and set the focus on the new "current" index.
-
Yes, do the same and add the logic to reset the state once you change the text searched.
-
What part are you failing on ?
-
Well, at the end i figured it out:
void Registry::on_Search_clicked(){ from++; QString text = ui->lineEdit_2->text(); if(text.isEmpty()) return; QModelIndexList indexes = ui->tableView->model()->match(ui->tableView->model()->index(0,0),Qt::EditRole,text,-1, Qt::MatchFlags(Qt::MatchContains|Qt::MatchWrap)); if(indexes.length() > from){ QModelIndex ix = indexes.at(from); ui->tableView->setCurrentIndex(ix); ui->tableView->scrollTo(ix); } QSound::play("/home/adan/Groostore/Resources/Sounds/Update.wav"); }
The int variable "from" is declared as public on the header.
Thanks!
-
There's no need to declare it as public, it's an internal implementation detail that should not be exposed.
Note that you don't seem to reset
from
anywhere so you will likely have strange results. Like for example always starting to show the second found entry.Another issue here is that you re-do the search every time so if you have a big table you are killing performances. You might want to take some time to re-think the search functionality.
-
@SGaist Hello, the from variable is being reseted on a QlineEdit slot called textChanged; where can i declare the from variable to avoid making it public? because everytime the Search button event is closed the variable int is reseted, and i need to keep it until textChanged is called. Thanks
-