PDA

View Full Version : QAbstractTableModel data insert issues



nategoofs
13th August 2007, 00:00
Im trying to add elements to a QAbstractTableModel via the QVariant data function like so

QVariant WTrackTableModel :: data(const QModelIndex &index, int role) const
{
TrackInfoObject *m_pTrackInfo = m_pTrackCollection->getTrack(index.row());
QStringList trackInfoList;
trackInfoList.append(m_pTrackInfo->getScoreStr());
trackInfoList.append(m_pTrackInfo->getTitle());
trackInfoList.append(m_pTrackInfo->getArtist());
trackInfoList.append(m_pTrackInfo->getType());
trackInfoList.append(m_pTrackInfo->getDurationStr());
trackInfoList.append(m_pTrackInfo->getBitrateStr());
trackInfoList.append(m_pTrackInfo->getBpmStr());
trackInfoList.append(m_pTrackInfo->getComment());

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

if (index.row() >= m_pTrackCollection->getSize())
return QVariant();

if (role == Qt::DisplayRole)
return trackInfoList;
else
return QVariant();
}

This code causes my program to crash. What I'm aiming to do is place the trackinfoobject's various get functions QString information into their respective columns (score, title, artist, etc.) what do I need to change to do so?

Thanks!

jpn
13th August 2007, 09:16
I suppose the crash is caused by dereferencing a null/invalid pointer. Perhaps you should check for index and pointer validities first. Also, I'd suggest constructing the data only in case of appropriate role (data() gets called a lot with various roles).



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

if (!m_pTrackCollection) // is always valid?
return QVariant();

if (index.row() >= m_pTrackCollection->getSize())
return QVariant();

if (role == Qt::DisplayRole)
{
TrackInfoObject *m_pTrackInfo = m_pTrackCollection->getTrack(index.row());
if (m_pTrackInfo) // is valid?
{
QStringList trackInfoList;
trackInfoList.append(...);
...
return trackInfoList;
}
}
return QVariant();
}