X
X
Xintrea2015-05-11 21:59:33
Qt
Xintrea, 2015-05-11 21:59:33

Qt: How to use QSortFilterProxyModel in conjunction with a view?

I have a project that until recently used a RecordTableModel extended from the QAbstractTableModel class . In this model, in addition to the overridden standard methods, additional methods were added that simplified the interaction between the view and the model:

void RecordTableModel::setTableData(RecordTableData *rtData)
RecordTableData *RecordTableModel::getTableData(void)
int RecordTableModel::addTableData(int mode,
                                   int pos,
                                   QMap<QString, QString> fields,
                                   QString text,
                                   QMap<QString, QByteArray> files)
void RecordTableModel::removeRowsByList(QVector<int> delIdx)

Now it's time to make sorting possible in the view. To do this, the RecordTableModel model must be replaced with QSortFilterProxyModel . Inside the view, I create an object of type RecordTableModel , create an object of type QSortFilterProxyModel , set the proxy model to the source model via setSourceModel() , and start trying to use the object of class QSortFilterProxyModel :
// Было
 recordModel=new RecordTableModel();
 setModel(recordModel);

 ...

 // Стало
 recordSourceModel=new RecordTableModel();

 recordModel=new QSortFilterProxyModel();
 recordModel->setSourceModel(recordSourceModel);

 setModel(recordModel);

But it turns out that the QSortFilterProxyModel class does not know how to call the methods of the Source-model class specified via setSourceModel().
How can I now manage data through a proxy model? After all, the methods of the main model are not available in it. I was trying to apply multiple inheritance by creating a class that inherited from RecordTableModel and QSortFilterProxyModel . But QAbstractTableModel (from which RecordTableModel is inherited ) and QSortFilterProxyModel have the same methods, and such code cannot be compiled.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Demonist, 2015-05-11
@Demonist

And you do not need to manage data through a proxy model. Do it directly with RecordTableModel and QSortFilterProxyModel will only sort.
Understand that the data should be stored only in the model (i.e. in the RecordTableModel) and it is she who should work with them, and no one else. QSortFilterProxyModel in this case will only change the display format of this data, but not the data itself.
I recommend to pull up knowledge about models. You can do this, for example, here .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question