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:
Code:
class Feed
{
public:
Feed();
Feed(const Feed &other);
Feed &operator=(const Feed &other);
};
feedModel.h:
Code:
class Feed;
#include <QAbstractListModel>
{
public:
private:
QList<Feed> list;
};
feedModel.cpp:
Code:
#include "feedModel.h"
#include "feed.h"
#include <QAbstractListModel>
int FeedModel
::rowCount ( const QModelIndex & parent
) const {
if (!parent.isValid())
return (-1);
else
return list.count();
}
{
if (!index.isValid())
if (role != Qt::DisplayRole)
// This is the hardest place for me!
// Feed *item = static_cast<Feed*>(index.internalPointer());
Feed item = static_cast<Feed>(list.at(index.row()));
switch (index.column()) {
case 0:
return item.stringUrl;
default:
}
}
Regards!
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.
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.
Re: A simplest ListModel... no example :(
Quote:
Originally Posted by tomek
Code:
// This is the hardest place for me!
// Feed *item = static_cast<Feed*>(index.internalPointer());
Feed item = static_cast<Feed>(list.at(index.row()));
list is a QList<Feed>, so list.at( x ) will return a reference to a Feed instance --- you don't need any casts.
Code:
int row = index.row();
if( 0 <= row && row < list.size() ) {
}
const Feed& item = list.at( row );
Re: A simplest ListModel... no example :(
Quote:
Originally Posted by tomek
Code:
int FeedModel
::rowCount ( const QModelIndex & parent
) const {
if (!parent.isValid())
return (-1);
else
return list.count();
}
Why so? parent is not valid for first level or hierarchy, thus you should return list.count() here...
Code:
int FeedModel
::rowCount ( const QModelIndex & parent
) const {
return list.count(); // the model is not hierarchical, so parent is always invalid
}
data() looks fine.
Please note that your model is read only!
Re: A simplest ListModel... no example :(
Thank you. Now it's all right!
I think I should improve my C++...
Have a good weekend.