1
我使用QAbstractListModel
在地圖上構建一些MapQuickItems,但我無法訪問模型中的數據。 示範頭是:QAbstractListModel委託在MapIteMVieW
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()
輸出)或母鹿沒有委託,我找不到原因。我訪問模型中的數據是否錯誤?我是否委託它錯了?