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:
class Feed
{
public:
Feed();
Feed(const Feed &other);
Feed &operator=(const Feed &other);
};
class Feed
{
public:
Feed();
Feed(const Feed &other);
Feed &operator=(const Feed &other);
QString stringUrl;
QDate lastFetched;
QString htmlContent;
};
To copy to clipboard, switch view to plain text mode
feedModel.h:
class Feed;
#include <QAbstractListModel>
{
public:
private:
QList<Feed> list;
};
class Feed;
#include <QAbstractListModel>
class FeedModel : public QAbstractListModel
{
public:
int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const;
private:
QList<Feed> list;
};
To copy to clipboard, switch view to plain text mode
feedModel.cpp:
#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:
}
}
#include "feedModel.h"
#include "feed.h"
#include <QAbstractListModel>
int FeedModel::rowCount ( const QModelIndex & parent ) const
{
if (!parent.isValid())
return (-1);
else
return list.count();
}
QVariant FeedModel::data ( const QModelIndex & index, int role ) const
{
if (!index.isValid())
return QVariant();
if (role != Qt::DisplayRole)
return QVariant();
// 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:
return QVariant();
}
}
To copy to clipboard, switch view to plain text mode
Regards!
Bookmarks