0

Scenario:

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?

4

1 に答える 1

1

モデルクラスを検索する方法によって少し異なります。通常、data() メソッドに Qt::UserRole を実装します。このロールは、ID のみを返すか、完全な構造へのポインターを返すことができます (Q_DECLARE_METATYPE を使用)。

次に、自分でモデル インデックスを処理して、次の呼び出しを行うことができます。

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

または QT の match(.) のようなメソッドを使用し、そこで Qt::UserRole を使用します。

3 番目の可能性は、ID を表示するかのように ID を返すことですが、ビューで列を非表示にします。

于 2016-02-08T08:57:02.477 に答える