Finding data(QString) of item in QAbstactTableModel
-
Hi!
My qt version is 5.14.2.
The environment is CentOS7.Now I'm trying to implement the ability to find data of an item in QTableView through QLineEdit.
But it is not easy to solve this problem.
Here's an example you've tried.
- proxy (QSortFilterProxyModel) - > mapToSource
- Match function (QAbstractItemModel) -> Finds only one column.
- FindItems function (QStandardItemModel) -> The row (), column () values of QModelIndex are printed -1.
I tried the above but could not solve it.
I want to find it on the whole table.Help me
-
Please show some code on what you you did. You can search your model by iterating over data() and then use the found index to select it in the view.
-
@Christian-Ehrlicher
I have implemented it as below as you told me.
But the number of data is now 100,000.
The rate of finding data is extremely slow.mainwindow.cpp
- QLineEdit slot function void MainWindow::slot_tableview_search() { QString str = ui->x_search_ledit->text().simplified(); for(int i = 0; i < ui->tableview->model()->rowCount(); i++) { for(int j = 0; j < ui->tableview->model()->columnCount(); j++) { if(str == m_tableview_model->data(ui->tableview->model()->index(i, j), Qt::DisplayRole)) { ui->tableview->setCurrentIndex(ui->tableview->model()->index(i, j)); ui->tableview->scrollTo(ui->tableview->model()->index(i, j)); } else { ; } } } } model.cpp - data() function QVariant Model::data(const QModelIndex &index, int role) const { if( role==Qt::DisplayRole ) { if((index.row() == rowHeaderList.size() - 1) && (rowList[index.row()][index.column()] == 0)) { return QString(); } else { return QString::number(rowList[index.row()][index.column()], 16); } } return QVariant::Invalid; }
-
Please use the
code
- tags so your code is readable. -
And do you find your data?
Why do you create the index three times?if(str == m_tableview_model->data(ui->tableview->model()->index(i, j), Qt::DisplayRole))
{
ui->tableview->setCurrentIndex(ui->tableview->model()->index(i, j));
ui->tableview->scrollTo(ui->tableview->model()->index(i, j)); -
I'm sorry. I've never done a code upload before, so I'm not good at it.
I want to find matching data in the entire table.
The search speed is slowed down when applying the above code.The reason why I use indexes three times
- Use of an if condition
- Scroll
- Data focus
-
Then create the index once and use it three times.
But you still did not answer my question - do you find your string you're looking for with this loop above?And again: please modify your first post to use
code
tags. -
Code tag is completed.
I'm trying to find a string entered in QLineEdit on the whole table.
so I ran the for loop to search the whole table. -
@keyboard_hellchang said in Finding data(QString) of item in QAbstactTableModel:
so I ran the for loop to search the whole table.
All fine, but do you find it? If not make sure data() returns the strings you're expecting. Maybe add a debug output to see what you get.
-
@keyboard_hellchang said in Finding data(QString) of item in QAbstactTableModel:
But the number of data is now 100,000.
The rate of finding data is extremely slow.-
How slow is "slow"? If you have 100k+ items to search through sequentially it will take a bit of time.
-
As @Christian-Ehrlicher has said, there is a place where you create the same
index()
three times, it might help if you only do that once, I don't know just how expensive that operation is. -
You convert every single element from a stored number to a string, to do the comparison. You might be better converting the string you are searching for to a number once, and then searching the elements for a number instead.
-
You might directly search your
rowList
data structure instead of going viadata()
. -
Finally, presumably you are not actually showing 100k+ items in a view, that would be too much for the user, so do you only need to search through whatever subset of the model you are showing in the view?
-
-
@Christian-Ehrlicher
I've almost solved this problem.
The feature is perfect except for the search speed. -
So what's your current solution? Did you implemented all what @JonB suggested? Esp. the conversions and the direct search in the model will help for sure.
-
@Christian-Ehrlicher
Yes, we have proceeded with all the proposals.
Also, this program is being used too well after completion.
Thank you!!