How to display selected columns in QTableView widget.
hi,
How can i display selected columns from a Model into my QTableView widget.
Say my model has 5 cloumns and i want o display two in a QTableView widget and
populate any other combo box widget with data from the third colunm of the Model.
Code:
int MyModel::columnCount(const QModelIndex& p) const
{
if ( p.isValid() || !sourceModel() )
return 0;
return 3;
}
Then three columns are shown in my QTableView widget but i want only two and data for the third to be put in a comboBox.
help!!!!
Regards,
GK
Re: How to display selected columns in QTableView widget.
Re: How to display selected columns in QTableView widget.
If you have a model with 5 columns and you only want to view 3 in your view, you have to add a subclass of QSortFilterProxyModel(or abstract if you prefer creating some of the functions yourself) between them.
On the subclass you create you need to implement:
Code:
bool MySortFilterProxyModel::filterAcceptsColumns(int source_column,
{
//return true for columns you want, else return false...
}
See documentation on QSortFilterProxyModel for example on filtering rows. it's the same for columns.
Then use the proxymodel on the view you have.
When accessing data through a selection model on the view you need to use the mapToSource and mapFromSource on the selectionmodel to find the corresponding item in your model. You can also use the QSortFilterProxyModel for sorting and filtering... which is something most applications want to have...
cheers,
Leif