Transforming and filtering a model
-
Hello!
I have a table model with the following structure:- first name
- second name
- location
- street
- telephone.
Necessary represent this structure differently. Need combine first name, second name to the full name and also, location, street to the full address, but need save the source structure model for filtering by each column.
I went to the following path and did:
- Source model (inherits from QAbstractTableModel)
- Filter model (inherits from QSortFilterProxyModel)
- Converter model (inherits from QIdentyProxyModel)
It turns out that the converter model refers to the filter model, filter model refers to the source model.
To set up model need make: tableView->setModel(converterModel)...
After that, all works, but it seems to me that the decision is not correct.
Help advice. Is there a better way? -
Hi and welcome to devnet,
Taking from what your wrote, from top to bottom:
- QTableView
- Converter Model
- Filter Model
- Source Model
That's the correct way.
-
@SGaist Hi! Thank you! I've got one more question...
There is universal method to get source index via converter Model?At the moment I'm doing:
- Get a index of filter model via a converter model
QModelIndex filterIndex = converterModel_->mapToSource(converterIndex);
- Get source index through the index of filter model
QModelIndex sourceIndex = filterModel_->mapToSource(filterIndex);
This method is not universal and designed for current situation.
-
What is your use case for that ?
-
@SGaist 3 models that described above are a set of my custom view. It may contain 2 models(source+filter) or 3 models(source+filter+converter). I'm want make an abstract interface for it. For example i'm want a shared slot double click in table. Need universal method to get source index via the converter model or the filter model. At the moment, i'm checking pointers to models and depending situation, I'm making a necessary actions.
void AbstractTableView::doubleClick(const QModelIndex &index) { int row; QModelIndex filterIndex; QModelIndex sourceIndex; //if a converterModel and filterModel was set if(converterModel_) { filterIndex = converterModel_->mapToSource(index); sourceIndex = filterModel_->mapToSource(filterIndex); //if only filterModel was set } else { sourceIndex = filterModel_->mapToSource(index); } //to make something with the resulting row row = sourceIndex.row(); editorWidget->editing(row, domainModel_->object(row)); }
I would like to know whether it is possible to do otherwise?
-
You don't need all of this. Just use the top-most index. Modifying its content will propagate it back to the bottom index.