View Full Version : QAbstractTableModel row hiding and sorting

18th August 2006, 02:41
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

QVariant StringListModel::data(const QModelIndex &index, int role) const
if (!index.isValid())
return QVariant();

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( )???
QString str;
switch( mDataModel[row][column].data.type( ) )
case QVariant::Double:
str = QString::sprintf( "%12.6g", mDataModel[row][column].data.toDouble( )
case QVariant::Int:
str = QString::sprintf( "%12d", mDataModel[row][column].data.toInt( ) );
str = mDataModel[row][column].data.toString( );

return str;
else if (role == Qt::BackgroundColorRole ) {
return mDataModel[row][column].color;

return QVariant( );

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?


18th August 2006, 08:51
QHeaderView::setSectionHidden(int, bool) (http://doc.trolltech.com/4.1/qheaderview.html#setSectionHidden)

23rd August 2006, 03:39
QHeaderView::setSectionHidden(int, bool) (http://doc.trolltech.com/4.1/qheaderview.html#setSectionHidden)
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...