QAbstractTableModel data insert issues
Im trying to add elements to a QAbstractTableModel via the QVariant data function like so
Code:
{
TrackInfoObject *m_pTrackInfo = m_pTrackCollection->getTrack(index.row());
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())
if (index.row() >= m_pTrackCollection->getSize())
if (role == Qt::DisplayRole)
return trackInfoList;
else
}
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!
Re: QAbstractTableModel data insert issues
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).
Code:
{
if (!index.isValid())
if (!m_pTrackCollection) // is always valid?
if (index.row() >= m_pTrackCollection->getSize())
if (role == Qt::DisplayRole)
{
TrackInfoObject *m_pTrackInfo = m_pTrackCollection->getTrack(index.row());
if (m_pTrackInfo) // is valid?
{
trackInfoList.append(...);
...
return trackInfoList;
}
}
}