Results 1 to 3 of 3

Thread: QAbstractTableModel row hiding and sorting

  1. #1
    Join Date
    Jan 2006
    Location
    Third rock from the sun
    Posts
    106
    Thanks
    17
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Post QAbstractTableModel row hiding and sorting

    I have sucessfully implemented a Table/Model view by subclassing QTableView and QAbstractTableModel. All adding, sorting and removal of table data is working great. However, I am trying to figure out how to hide a row in the table using the setRowHidden( ) fucntion. I have setup a structure in the TableModel to flag if the row is hidden or not, but the data( ) function returns a empty QVariant( ) if the cell is supposed to be hidden.
    The data is stored in a 2D Vector for easy sorting. I am using a struct with a QVariant, QColor, and bool shown (hidden/not hidden) to store the data attributes. Here is the code from the data( ) function. Please ignore any obvious mistakes, I typed this in from memory since I dont have access to the code at home!

    Qt Code:
    1. QVariant StringListModel::data(const QModelIndex &index, int role) const
    2. {
    3. if (!index.isValid())
    4. return QVariant();
    5.  
    6. int row = index.row();
    7. int column = index.column();
    8.  
    9. if (role == Qt::DisplayRole) {
    10. if( mDataModel[row][column].shown == false )
    11. return QVariant(); // <==== Return somthing other than QVariant( )???
    12. else
    13. QString str;
    14. switch( mDataModel[row][column].data.type( ) )
    15. {
    16. case QVariant::Double:
    17. str = QString::sprintf( "%12.6g", mDataModel[row][column].data.toDouble( )
    18. case QVariant::Int:
    19. str = QString::sprintf( "%12d", mDataModel[row][column].data.toInt( ) );
    20. default:
    21. str = mDataModel[row][column].data.toString( );
    22.  
    23. return str;
    24. }
    25. else if (role == Qt::BackgroundColorRole ) {
    26. return mDataModel[row][column].color;
    27. }
    28.  
    29. return QVariant( );
    30. }
    To copy to clipboard, switch view to plain text mode 

    What I am asking is if there is an easy way to not display the data row in an Model without completly removing the entries?

    Thanks

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QAbstractTableModel row hiding and sorting

    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Location
    Third rock from the sun
    Posts
    106
    Thanks
    17
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractTableModel row hiding and sorting

    Quote Originally Posted by jpn
    The setSectionHidden( ) command hid the row, just like setRowHidden( ) but I still get the same problem when the row is sorted by the column headers. When the column is sorted, any hidden rows are sorted and re-displayed, despite being hidden. Is there a way to flag the iitem in the data model to be hidden? As in my example code above, I set a struct to contain a boolean "shown" value that sets if the item should be hidden or not. But when the data( ) function is called, the item is redisplayed or if I return a NULL QVariant( ) the item is still show n but blank. I am at a loss of how to keep these rows hidden...

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.