2016-05-31 31 views
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()輸出)或母鹿沒有委託,我找不到原因。我訪問模型中的數據是否錯誤?我是否委託它錯了?

回答

0

這不是完整的代碼,你也需要發佈.cpp文件。但我會猜測,我認爲你沒有正確實現rowCount和data函數 - 它們應該使用l_adventuresOnMap成員變量爲這兩個函數返回正確的數據。