PDA

View Full Version : QAbstractTableModel SetRowCount problem



gutiory
25th November 2010, 07:05
Hello.
I have on class that subclass QAbstractTableModel, like this:

class CTableModel : public QAbstractTableModel
{
Q_OBJECT

public:
CTableModel(QObject *parent=0);
virtual ~CTableModel();

int rowCount(const QModelIndex &parent = QModelIndex()) const;
void setRowCount(int numRows);
int columnCount(const QModelIndex &parent = QModelIndex()) const;
void setColumnCount(int numColumns);

QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
bool setData(const QModelIndex &index, const QVariant & value, int role = Qt::EditRole );

void setHorToolTipList(QStringList lista);
Qt::ItemFlags flags ( const QModelIndex & index ) const;

QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
bool setHeaderData( int section, Qt::Orientation orientation, const QVariant & value, int role = Qt::EditRole);

private:
QVector< QVector <QString> > items;
QStringList horHeaders,verHeaders,horToolTip;

};

And the .cpp,


int CTableModel::rowCount(const QModelIndex &parent) const
{
return items.count();
}

void CTableModel::setRowCount(int numRows)
{
if (numRows > rowCount())
{
int tamAnt = rowCount();
items.resize(numRows);

for (int i=tamAnt;i<numRows;i++)
items[i].resize(columnCount());
}
else
{
if (numRows < rowCount())
{
items.resize(numRows);
}
}

}

int CTableModel::columnCount(const QModelIndex &parent) const
{
if (items.count() == 0)
return 0;
return items[0].count();
}

void CTableModel::setColumnCount(int numColumns)
{
for(int i=0;i<items.count();i++)
items[i].resize(numColumns);
}

QVariant CTableModel::data(const QModelIndex &index, int role) const
{
if (index.isValid())
{
if (role == Qt::DisplayRole)
return items[index.row()][index.column()];
}
return QVariant();

}

bool CTableModel::setData(const QModelIndex &index, const QVariant & value, int role)
{
if (index.isValid() && role == Qt::EditRole)
{
items[index.row()][index.column()] = value.toString();
emit dataChanged(index,index);
return true;
}
return false;
}

void CTableModel::setHorToolTipList(QStringList lista)
{
horToolTip = lista;
}

Qt::ItemFlags CTableModel::flags ( const QModelIndex & index ) const
{

return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
}

QVariant CTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
{
if (orientation == Qt::Horizontal)
{
if(role == Qt::ToolTipRole)
{
return horToolTip[section];
}
}
return QVariant();
}
if (role == Qt::DisplayRole)
{
if (orientation == Qt::Horizontal)
{
return horHeaders.at(section);
}
else
{
return verHeaders.at(section);
}
}
}


bool CTableModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant & value, int role)
{
if (role != Qt::EditRole)
return false;
if (orientation == Qt::Horizontal)
{
if (section > horHeaders.count()-1)
horHeaders.append(value.toString());
else
horHeaders[section] = value.toString();
emit headerDataChanged(orientation,section,section);
return true;
}
else
{
if (section > verHeaders.count()-1)
verHeaders.append(value.toString());
else
verHeaders[section] = value.toString();
emit headerDataChanged(orientation,section,section);
return true;
}

}


I set the numbers of rows with setRowCount and the first time it works fine. But when I try to change it again, it doesn't work anymore. It weird because when I call model->rowCount(), the result is ok, but the view doesn't update the numbers of row.

Thanks a lot.
Regards.

JPNaude
25th November 2010, 07:27
If you change the number of rows emit the layoutChanged() signal in the setRowCount() function. The same goes for the setColumnCount() function.

gutiory
25th November 2010, 07:48
Thanks a lot JPNaude!!.
I works great.