Search QTableView with a value(e.g. "ID") which is not shown in the table
Scenario:
Say, I have a person class
Code:
class Person{
int id; // only unique value
etc etc
}
The list of Person class is maintained in class called MyAllData class which is outside of my model class
The model class I am using; inherits QAbstractTableModel
MyCustomModelClass has a reference to the person list. The table does not display the ID number of a person. But it is the only thing with which one can identify a person separately. If I want to search my table data with ID then how can I do that?
Re: Search QTableView with a value(e.g. "ID") which is not shown in the table
Two options:
1) Add ID as a column in the model but hide it in the view, then use QAbstractItemModel::match for searching
2) Implement a search function in your model that gets and ID value and returns the QModelIndex for the respective row
Cheers,
_
Re: Search QTableView with a value(e.g. "ID") which is not shown in the table
Quote:
Originally Posted by
anda_skoa
2) Implement a search function in your model that gets and ID value and returns the QModelIndex for the respective row
_
I understood Option 1.
Unfortunately I did not understand Option 2 :(
Re: Search QTableView with a value(e.g. "ID") which is not shown in the table
For option 2 you would add a method for getting an index of the model for a given ID.
You could do that outside the model but it is probably more convenient to put it into the model class.
Something like
Code:
QModelIndex MyCustomModelClass
::indexForID(int id
) const {
for (int i = 0; i < data.count(); ++i) {
if (data.at(i).id == id) {
return index(i, 0);
}
}
}
I.e. find the row the person with the given ID is in, return the model index for that row.
"data" is your reference to the MyAllData object, assuming that it can be queried for number of entries and can be asked for a specific entry.
You could also have a findById function in the data class and then use its return value to create the index, etc.
Cheers,
_