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! :D
Code:
{
if (!index.isValid())
int row = index.row();
int column = index.column();
if (role == Qt::DisplayRole) {
if( mDataModel[row][column].shown == false )
return QVariant();
// <==== Return somthing other than QVariant( )??? else
switch( mDataModel[row][column].data.type( ) )
{
str
= QString::sprintf( "%12.6g", mDataModel
[row
][column
].
data.
toDouble( ) str
= QString::sprintf( "%12d", mDataModel
[row
][column
].
data.
toInt( ) );
default:
str = mDataModel[row][column].data.toString( );
return str;
}
else if (role == Qt::BackgroundColorRole ) {
return mDataModel[row][column].color;
}
}
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
Re: QAbstractTableModel row hiding and sorting
Re: QAbstractTableModel row hiding and sorting
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...