Greetings,

Using the TransposeProxy example as a template I've subclassed QSortFilterProxyModel to display table-based model data in both QTableView and QTreeView widgets. The structure of the model is fixed since it is provided by someone else. (I'm using Qt 4.6.2 on a Win XP system.)

QTreeView problem: I want to display a subset of the model header strings in a 'Property' column and the corresponding data in a 'Value' column. The data in the model, for example, is 1 row, 25 entries but I only want to display columns 16-24 in the QTreeView. What I am seeing is ALL of the headers (indexes 0-24) in the tree and NO data values.

MODEL
+---+---+---+-...-+----+-...-+----+
| 0 | 1 | 2 | ... | 16 | ... | 24 | HEADER
+---+---+---+-...-+----+-...-+----+
| a | b | c | ... | aa | ... | ii | DATA
+---+---+---+-...-+----+-...-+----+


So, I have two problems:
  1. How to display only a subset of the model.
  2. How to get the data to display.

I've overridden the filterAcceptsRow() and filterAcceptsColumn() functions in my proxy. The row appears to be filtered correctly and the columns appear to reject indexes 0-15. But, headers 0-15 still appear. Again, no data values appear.

Clearly I don't fully understand how to handle the proxy functions to deal with this situation. Might this be a problem with the mapToSource() override? Anyone have suggestions?

Qt Code:
  1. PropInspectProxyModel::PropInspectProxyModel( const DCID& dcid,
  2. const CID& cid,
  3. QObject* pObject )
  4. : QSortFilterProxyModel( pObject )
  5. , m_dcid( dcid )
  6. , m_cid( cid )
  7. , m_nNumProps( 10 )//TODO: make this a parameter
  8. {
  9. // Set this property to true to have the proxy model re-filter whenever
  10. // the contents of the source model change.
  11. setDynamicSortFilter( true );
  12.  
  13. //TODO: why doesn't setting the headers here work? No headers appear at all!
  14. setHeaderData( 0, Qt::Horizontal, QVariant( "Property" ), Qt::DisplayRole );
  15. setHeaderData( 1, Qt::Horizontal, QVariant( "Value" ), Qt::DisplayRole );
  16.  
  17. m_dcidCol = global.worldModelHelper->findFirstDCIDCol( m_dcid );
  18. m_nMaxColIdx = m_dcidCol + m_nNumProps;
  19. }
  20.  
  21. QModelIndex PropInspectProxyModel::mapToSource( const QModelIndex& proxyIndex ) const
  22. {
  23. return sourceModel()->index( proxyIndex.column(), proxyIndex.row() );
  24. }
  25.  
  26. bool PropInspectProxyModel::filterAcceptsRow( int source_row,
  27. const QModelIndex& source_parent ) const
  28. {
  29. QStandardItemModel* pModel = qobject_cast<QStandardItemModel*>(sourceModel());
  30. QStandardItem* pStdItem = pModel->item( source_row );
  31.  
  32. if ( pStdItem != NULL )
  33. {
  34. CID cid;
  35.  
  36. ConcertItemAdditionC* pItem = dynamic_cast<ConcertItemAdditionC*>( pStdItem );
  37. if ( pItem != NULL )
  38. {
  39. cid = pItem->getCID();
  40.  
  41. // Filter on CID
  42. if ( cid == m_cid )
  43. return true;
  44. }
  45. }
  46.  
  47. return false;
  48. }
  49.  
  50. bool PropInspectProxyModel::filterAcceptsColumn( int source_column,
  51. const QModelIndex& source_parent ) const
  52. {
  53. if ( (source_column >= m_dcidCol) && (source_column <= m_nMaxColIdx) )
  54. return true;
  55.  
  56. return false;
  57. }
To copy to clipboard, switch view to plain text mode