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!
{
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;
}
}
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( )???
else
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( ) );
default:
str = mDataModel[row][column].data.toString( );
return str;
}
else if (role == Qt::BackgroundColorRole ) {
return mDataModel[row][column].color;
}
return QVariant( );
}
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
Bookmarks