QAbstractListModel在MapIteMVieW中委托

时间:2016-05-31 08:19:56

标签: c++ qt qml

我正在使用QAbstractListModel在地图上构建一些MapQuickItems,但是我在访问模型中的数据时遇到了问题。 Model标题如下:

class AdventureOnMap
{
public:
AdventureOnMap(const int &adventureId, const int &tagId, const QString &name, const QString &desc, const QString &clue, const int &award, const double &geoLat, const double &geoLong);

int     adventureId() const;
int     tagId() const;
QString name() const;
QString desc() const;
QString clue() const;
int     award() const;
double  geoLat() const;
double geoLong() const;

private:
int     l_adventureId;
int     l_tagId;
QString l_name;
QString l_desc;
QString l_clue;
int     l_award;
double  l_geoLat;
double  l_geoLong;
};

class AdventureOnMapModel : public QAbstractListModel
{
Q_OBJECT
public:
enum AdventureOnMapRoles {
    AdventureIdRole  = Qt::UserRole + 1,
    TagIdRole,
    NameRole,
    DescRole,
    ClueRole,
    AwardRole,
    GeoLatRole,
    GeoLongRole
};

AdventureOnMapModel(QObject *parent = 0);

void addAdventureOnMap(const AdventureOnMap &AdventureOnMap);

int rowCount(const QModelIndex & parent = QModelIndex()) const;

QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;

protected:
QHash<int, QByteArray> roleNames() const;
private:
QList<AdventureOnMap> l_adventuresOnMap;
};

我通过Q_INVOKABLE访问模型,返回指向此模型的指针,如下所示:

Q_INVOKABLE AdventureOnMapModel* buildAdventuresOnMap() const;

我在QML中将其称为:

MapItemView {
    model: thisAdvendture.buildAdventuresOnMap()
//this returns AdventureOnMapModel*
    delegate: MapQuickItem {
        coordinate: QtPositioning.coordinate(geoLat,geoLong)
        //Anhors pointer right in the middle of bottom
        anchorPoint.x: image.width * 0.5
        anchorPoint.y: image.height

        sourceItem: Column {
            Image { id: image; source: "../Resources/marker.png" }
            Text { text: name;
                    font.bold: true
            }

但是模型似乎是空的(尽管我看到它被qDebug()输出填充)或者没有委托,我找不到原因。我访问模型中的数据错了吗?我委托错了吗?

1 个答案:

答案 0 :(得分:0)

这不是完整的代码,您还需要发布.cpp文件。但我会猜测,我认为你还没有正确实现rowCount和数据函数 - 他们应该使用l_adventuresOnMap成员变量为这两个函数返回正确的数据。