2016-05-31 8 views
1

Ich verwende QAbstractListModel, um einige MapQuickItems auf einer Karte zu erstellen, aber ich habe Probleme beim Zugriff auf die Daten im Modell. Die Modell-Header sind wie:QAbstractListModel Delegieren in 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; 
}; 

ich das Modell durch ein Q_INVOKABLE zugreifen, die einen Zeiger auf dieses Modell zurückgibt, wie:

Q_INVOKABLE AdventureOnMapModel* buildAdventuresOnMap() const;

Und ich nenne es in QML als:

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 
      } 

Aber das Modell scheint leer zu sein oder doe (obwohl ich es in qDebug() Ausgabe gefüllt sehen ist) nicht Delegierter, und ich kann den Grund nicht finden. Habe ich auf die Daten im Modell falsch zugegriffen? Verteile ich es falsch?

Antwort

0

Dies ist kein vollständiger Code, Sie müssen auch CPP-Datei. Aber ich nehme an, ich denke, Sie haben die Funktionen rowCount und data nicht korrekt implementiert - sie sollten die Membervariable l_adventuresOnMap verwenden, um die korrekten Daten für diese beiden Funktionen zurückzugeben.