Results 1 to 2 of 2

Thread: QAbstractTableModel data insert issues

  1. #1
    Join Date
    Aug 2007
    Posts
    19
    Thanks
    5
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default QAbstractTableModel data insert issues

    Im trying to add elements to a QAbstractTableModel via the QVariant data function like so
    Qt Code:
    1. QVariant WTrackTableModel :: data(const QModelIndex &index, int role) const
    2. {
    3. TrackInfoObject *m_pTrackInfo = m_pTrackCollection->getTrack(index.row());
    4. QStringList trackInfoList;
    5. trackInfoList.append(m_pTrackInfo->getScoreStr());
    6. trackInfoList.append(m_pTrackInfo->getTitle());
    7. trackInfoList.append(m_pTrackInfo->getArtist());
    8. trackInfoList.append(m_pTrackInfo->getType());
    9. trackInfoList.append(m_pTrackInfo->getDurationStr());
    10. trackInfoList.append(m_pTrackInfo->getBitrateStr());
    11. trackInfoList.append(m_pTrackInfo->getBpmStr());
    12. trackInfoList.append(m_pTrackInfo->getComment());
    13.  
    14. if (!index.isValid())
    15. return QVariant();
    16.  
    17. if (index.row() >= m_pTrackCollection->getSize())
    18. return QVariant();
    19.  
    20. if (role == Qt::DisplayRole)
    21. return trackInfoList;
    22. else
    23. return QVariant();
    24. }
    To copy to clipboard, switch view to plain text mode 

    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!
    Last edited by nategoofs; 13th August 2007 at 01:24.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Exclamation 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).

    Qt Code:
    1. QVariant WTrackTableModel :: data(const QModelIndex &index, int role) const
    2. {
    3. if (!index.isValid())
    4. return QVariant();
    5.  
    6. if (!m_pTrackCollection) // is always valid?
    7. return QVariant();
    8.  
    9. if (index.row() >= m_pTrackCollection->getSize())
    10. return QVariant();
    11.  
    12. if (role == Qt::DisplayRole)
    13. {
    14. TrackInfoObject *m_pTrackInfo = m_pTrackCollection->getTrack(index.row());
    15. if (m_pTrackInfo) // is valid?
    16. {
    17. QStringList trackInfoList;
    18. trackInfoList.append(...);
    19. ...
    20. return trackInfoList;
    21. }
    22. }
    23. return QVariant();
    24. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

Similar Threads

  1. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 12:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.