我正在使用QAbstractListModel
在地图上构建一些MapQuickItem,但是在访问模型中的数据时遇到了麻烦。
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()
输出中)或没有委托,而且我找不到原因。我访问模型中的数据是否错误?我把它委派错了吗? 最佳答案
这不是完整的代码,您还需要发布.cpp文件。但是我会猜测一下,我认为您没有正确实现rowCount和data函数-它们应该使用l_adventuresOnMap成员变量来为这两个函数返回正确的数据。
关于c++ - MapIteMVieW中委派的QAbstractListModel,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37540430/