PDA

View Full Version : QTableView not refreshing



steg90
8th May 2007, 14:46
Hi,

I'm using my own model derived from QAbstractTableModel. My class looks something like :



class DATreeModel : public QAbstractTableModel
{
Q_OBJECT

public:
DATreeModel(const QStringList &strings, QObject *parent = 0)
: QAbstractTableModel(parent), stringList(strings) {}

DATreeModel(QObject *parent = 0);
~DATreeModel();

int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &) const;
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
void setCanData( const QString& strData );
void SetHeaders( QStringList& strHeaders );

private:
QStringList stringList;
QList<QString> m_Data;
QStringList strHeaderList;
};


This model is just read only so no need to override setData. Now the following snippet of code is used to set the data :



QVariant DATreeModel::data( const QModelIndex& index, int role) const
{
QVariant data;

if (!index.isValid())
return QVariant();


if (index.row() > (m_Data.size() / 4) )
{
return QVariant();
}

if(index.column() > 3 )
{
return QVariant();
}

if( role == Qt::DisplayRole )
{
// just return data at given column
int nPos = index.column() + ( 4 * index.row() );
if( nPos < m_Data.size() )
data = m_Data.at( nPos );

}

return data;
}


And the following is used to insert new data :


void DATreeModel::setCanData( const QString& strData )
{
m_Data.append(strData);
QModelIndex nindex = index(m_Data.size(),0);;
emit dataChanged(nindex, nindex );
}


Alas, the table view is not 'refreshed' unless I click on the cells within it, I thought this would be automatic?

Any help would be much appreciated.

Steve

jpn
8th May 2007, 14:51
Alas, the table view is not 'refreshed' unless I click on the cells within it, I thought this would be automatic?
Make the model aware of the insertion with:

QAbstractItemModel::beginInsertRows()
QAbstractItemModel::endInsertRows()

steg90
8th May 2007, 15:06
Thanks,

Do I need to implement these functions in my model?

Regards,
Steve

steg90
8th May 2007, 15:19
Hi again,

I've now done this, thanks JPN ;)

wysota
8th May 2007, 20:37
An alternative is to emit layoutChanged() after inserting new data.