Home Ask Login Register

Developers Planet

Your answer is one click away!

sami1592 February 2016

Search QTableView with a value(e.g. ID) which is not displayed


Say, I have a person class

class Person{
    int id;          // only unique value, NOT displayed
    QString name;    // displayed
    QString address; // displayed 
    QString age;     // displayed
    etc etc          // displayed

The model class I am using; inherits QAbstractTableModel - MyCustomModelClass : QAbstractTableModel . MyCustomModelClass has a reference to the person list. Person list is maintained in class called MyAllData which is outside of my model class.

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?


IceFire February 2016

It depends a bit on which method you would like to search your model class with. Usually, I would implement a Qt::UserRole in your data() method. This role could either return your ID only or a pointer to your complete structure (using Q_DECLARE_METATYPE).

Then, you can either work your way through the model indices on your own, calling

model->data(idx, Qt::UserRole).toValue<Person*>()

or use methods like QT's match(.) and use Qt::UserRole there.

A third possibility would be to return the ID as if you would like to display it, but hiding the column in your view.

Post Status

Asked in February 2016
Viewed 2,385 times
Voted 11
Answered 1 times


Leave an answer

Quote of the day: live life