Results 1 to 6 of 6

Thread: A simplest ListModel... no example :(

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Poznań, Poland
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question A simplest ListModel... no example :(

    Hi!
    I'm trying to make a simplest ListModel accessing QList<Class>.
    Could someone take a look and tell me how it should be written?
    feed.h:
    Qt Code:
    1. class Feed
    2. {
    3. public:
    4. Feed();
    5. Feed(const Feed &other);
    6. Feed &operator=(const Feed &other);
    7.  
    8. QString stringUrl;
    9. QDate lastFetched;
    10. QString htmlContent;
    11. };
    To copy to clipboard, switch view to plain text mode 

    feedModel.h:
    Qt Code:
    1. class Feed;
    2. #include <QAbstractListModel>
    3. class FeedModel : public QAbstractListModel
    4. {
    5. public:
    6. int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
    7. QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const;
    8. private:
    9. QList<Feed> list;
    10.  
    11. };
    To copy to clipboard, switch view to plain text mode 

    feedModel.cpp:
    Qt Code:
    1. #include "feedModel.h"
    2. #include "feed.h"
    3.  
    4. #include <QAbstractListModel>
    5.  
    6. int FeedModel::rowCount ( const QModelIndex & parent ) const
    7. {
    8. if (!parent.isValid())
    9. return (-1);
    10. else
    11. return list.count();
    12. }
    13.  
    14. QVariant FeedModel::data ( const QModelIndex & index, int role ) const
    15. {
    16. if (!index.isValid())
    17. return QVariant();
    18.  
    19. if (role != Qt::DisplayRole)
    20. return QVariant();
    21. // This is the hardest place for me!
    22. // Feed *item = static_cast<Feed*>(index.internalPointer());
    23. Feed item = static_cast<Feed>(list.at(index.row()));
    24.  
    25. switch (index.column()) {
    26. case 0:
    27. return item.stringUrl;
    28. default:
    29. return QVariant();
    30. }
    31. }
    To copy to clipboard, switch view to plain text mode 
    Regards!
    Last edited by tomek; 6th January 2006 at 23:47.

Similar Threads

  1. Simplest example for QGraphicsPixmapItem
    By sincnarf in forum Qt Programming
    Replies: 7
    Last Post: 21st September 2012, 14:24
  2. simplest / best way to fuse several files with QT?
    By Havard in forum Qt Programming
    Replies: 3
    Last Post: 18th June 2007, 10:06

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.