Results 1 to 6 of 6

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

  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; 7th January 2006 at 00:47.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: A simplest ListModel... no example :(

    You provided invalid addresses for those files. Please attach them properly using the attachment feature. Or post your code here using the [code] tag.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: A simplest ListModel... no example :(

    Quote Originally Posted by tomek
    Could someone take a look and tell me how it should be written?
    You can find a short tutorial here.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: A simplest ListModel... no example :(

    Quote Originally Posted by tomek
    Qt Code:
    1. // This is the hardest place for me!
    2. // Feed *item = static_cast<Feed*>(index.internalPointer());
    3. Feed item = static_cast<Feed>(list.at(index.row()));
    To copy to clipboard, switch view to plain text mode 
    list is a QList<Feed>, so list.at( x ) will return a reference to a Feed instance --- you don't need any casts.
    Qt Code:
    1. int row = index.row();
    2. if( 0 <= row && row < list.size() ) {
    3. return QVariant();
    4. }
    5. const Feed& item = list.at( row );
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: A simplest ListModel... no example :(

    Quote Originally Posted by tomek
    Qt Code:
    1. int FeedModel::rowCount ( const QModelIndex & parent ) const
    2. {
    3. if (!parent.isValid())
    4. return (-1);
    5. else
    6. return list.count();
    7. }
    To copy to clipboard, switch view to plain text mode 
    Why so? parent is not valid for first level or hierarchy, thus you should return list.count() here...

    Qt Code:
    1. int FeedModel::rowCount ( const QModelIndex & parent ) const
    2. {
    3. return list.count(); // the model is not hierarchical, so parent is always invalid
    4. }
    To copy to clipboard, switch view to plain text mode 

    data() looks fine.

    Please note that your model is read only!

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

    Default Re: A simplest ListModel... no example :(

    Thank you. Now it's all right!
    I think I should improve my C++...
    Have a good weekend.

Similar Threads

  1. Simplest example for QGraphicsPixmapItem
    By sincnarf in forum Qt Programming
    Replies: 7
    Last Post: 21st September 2012, 15:24
  2. simplest / best way to fuse several files with QT?
    By Havard in forum Qt Programming
    Replies: 3
    Last Post: 18th June 2007, 11: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.