PDA

View Full Version : How to display selected columns in QTableView widget.



kaushal_gaurav
8th August 2008, 06:33
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.



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

spirit
8th August 2008, 08:06
maybe this method will help


QModelIndexList QItemSelectionModel::selectedColumns ( int row = 0 ) const

http://doc.trolltech.com/4.4/qitemselectionmodel.html#selectedColumns (QItemSelectionModel)

luf
8th August 2008, 09:30
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:


bool MySortFilterProxyModel::filterAcceptsColumns(int source_column,
const QModelIndex &source_parent) const
{
//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