Results 1 to 5 of 5

Thread: TransposeProxy-based filtering problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question TransposeProxy-based filtering problem

    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 

  2. #2
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: TransposeProxy-based filtering problem

    You don't need the mapToSource. Get rid of it.

  3. #3
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: TransposeProxy-based filtering problem

    I think it's necessary, without mapToSource() the program crashes when setting the model in the QTreeView.

    Qt Code:
    1. PropInspectProxyModel* proxyModel =
    2. new PropInspectProxyModel( dcid, cid );
    3. if ( proxyModel == NULL )
    4. {
    5. // report error
    6. return;
    7. }
    8.  
    9. proxyModel->setSourceModel( m_pSourceModel );
    10. m_pPropTree->setModel( proxyModel ); // crashes here!
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: TransposeProxy-based filtering problem

    I have been able to remove the unwanted header values from the rows (0-15) in the tree view. Unforunately those rows remain empty instead of being filled with the filtered values.

    I have also found that the data() function can get both the header values AND data values.
    Qt Code:
    1. QVariant PropInspectProxyModel::data( const QModelIndex& index, int role ) const
    2. {
    3. QModelIndex source_index = mapToSource( index );
    4. if ( index.isValid() && !source_index.isValid() )
    5. return QVariant();
    6.  
    7. ///////////////////////////////////////////////////////////////////////////
    8. // How do I know when to get the header data vs. getting the data values???
    9.  
    10. // Get the Property header string
    11. QVariant vHdr = sourceModel()->headerData( index.row(), Qt::Horizontal, role );
    12.  
    13. // Get the data values
    14. QVariant v = sourceModel()->data( source_index, role );
    15.  
    16. return v;
    17. }
    To copy to clipboard, switch view to plain text mode 
    Two unresolved issues remain:
    1. How can I get my headers to fill rows 0-9 instead of 16-25?
    2. How do I know when to get header values or data values in the data() override function?
    Examples
    1. HeadersHeadersOnly..JPG
    2. DataDataOnly..JPG

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: TransposeProxy-based filtering problem

    What does rowCount() on the proxy model return? It should be the number of rows for the given parent index left after the filter is applied. Similarly for the columnCount(). If this is not the case then look at your filter implementations. Is there a hierarchy in the model... are all parent model indexes invalid or not?

    When a request is made on the filtered side of the model via data() you need to map the row number to the base model row e.g. proxy row 0 maps to row 16 in the source, 1 to 17, etc. mapToSource() should do this and mapFromSource() should do the inverse. I would have thought the default proxy implementation does this for you and you should not need to reimplement these.

    The view should never call data() to retrieve headers, that's what headerData() is for. The proxy default implementation should also be returning the header data from the base model for rows/columns that get past the filter.

Similar Threads

  1. QCA based application deployment problem
    By npclaudiu in forum Installation and Deployment
    Replies: 1
    Last Post: 14th May 2010, 10:10
  2. Qwt based project compile problem
    By MarkoSan in forum Qwt
    Replies: 0
    Last Post: 28th September 2009, 15:08
  3. Filtering QSqlQueryModel
    By mourad in forum Qt Programming
    Replies: 4
    Last Post: 2nd August 2009, 09:32
  4. Filtering a TreeWidget
    By soul_rebel in forum Qt Programming
    Replies: 6
    Last Post: 26th January 2008, 20:08
  5. processevents, filtering events
    By sreedhar in forum Qt Programming
    Replies: 1
    Last Post: 24th June 2006, 22:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.