Hi,
I'm using my own model derived from QAbstractTableModel. My class looks something like :
{
Q_OBJECT
public:
~DATreeModel();
QVariant headerData
(int section, Qt
::Orientation orientation,
int role
) const;
void setCanData( const QString& strData );
void SetHeaders( QStringList& strHeaders );
private:
QList<QString> m_Data;
};
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;
};
To copy to clipboard, switch view to plain text mode
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 {
if (!index.isValid())
if (index.row() > (m_Data.size() / 4) )
{
}
if(index.column() > 3 )
{
}
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;
}
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;
}
To copy to clipboard, switch view to plain text mode
And the following is used to insert new data :
void DATreeModel::setCanData( const QString& strData )
{
m_Data.append(strData);
emit dataChanged(nindex, nindex );
}
void DATreeModel::setCanData( const QString& strData )
{
m_Data.append(strData);
QModelIndex nindex = index(m_Data.size(),0);;
emit dataChanged(nindex, nindex );
}
To copy to clipboard, switch view to plain text mode
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
Bookmarks