【发布时间】:2019-06-15 21:10:45
【问题描述】:
我对 Qt 很陌生,将模型传递给视图时遇到问题。 我的视图有一堆按钮和一个带有一些标记的地图,这些标记的纬度/经度来自我的模型。 点击按钮应该更新地图上的标记(删除一些和/或显示新的)。
问题是:当我的模型(QList)在 C++ 端更新时,QML 端没有。
(我知道这种问题似乎已经被问过了,但是在阅读了不同的答案之后,我无法清楚地了解我是否可以通过更聪明的方式来调用 setContextProperty() 或者如果我必须使用诸如发出信号和绑定属性之类的东西,在阅读了一些文档后我也无法清楚地看到)
架构如下:
具有 QApplication 实例化和 MainWindow 的主类(MainWindow 是自定义 QMainWindow 类)。应用程序被执行并显示窗口。
-
带有 updateMap() 方法的 Mapwidget 类(自定义 QQuickWidget 类):
- 对用户界面上的按钮点击做出反应
- 更新模型(QList)
- 使用 setContextProperty() 方法将更新后的模型传递给 视图
MainWindow 类有一个 Mapwidget 属性
到目前为止我尝试过的事情:
在调用 setSource() 方法之前在 Mapwidget 构造函数中调用 setContextProperty() 时,会考虑模型。所以我用于将模型传递给视图的语法应该是正确的。问题似乎是之后对 setContextProperty() 的任何调用(在这种情况下:在 updateMap() 方法中)都没有传递给 QML 文件。
在不同级别(Mapwidget 类、MainWindow 类)调用 setContextProperty(),结果是一样的,应用首次启动后从不考虑。
我已经测试了模型,并且知道它确实在 updateMap() 方法中得到了更新,只是似乎更新没有传输到 QML 文件中。
QML 文件:
Item {
width: 1200
height: 1000
visible: true
Plugin {
id: osmPlugin
name: "osm"
}
Map {
id: map
anchors.fill: parent
plugin: osmPlugin
center: QtPositioning.coordinate(45.782074, 4.871263)
zoomLevel: 5
MapItemView {
model : myModel
delegate: MapQuickItem {
coordinate:QtPositioning.coordinate(
model.modelData.lat,model.modelData.lon)
sourceItem: Image {
id:image_1
source: <picturePath>
}
anchorPoint.x: image_1.width / 2
anchorPoint.y: image_1.height / 2
}
}
}
Mapwidget 类:
mapwidget::mapwidget(QWidget *parent) : QQuickWidget(parent)
{
this->setSource(QUrl(QStringLiteral("qrc:/main.qml")));
}
void mapwidget::updateMap(QList<QObject *> &data)
{
/**
DO OPERATIONS TO UPDATE data
Each append has the following form :
data.append(new DataObject(someLatitude, someLongitude))
*/
this->rootContext()->setContextProperty("myModel", QVariant::fromValue(data));
}
在 updateMap() 方法中,附加到列表的 QObjects 属于自定义 Class DataObject :
class DataObject : public QObject
{
Q_OBJECT
Q_PROPERTY(double lat READ lat WRITE setLat)
Q_PROPERTY(double lon READ lon WRITE setLon)
public:
explicit DataObject(QObject *parent = nullptr);
DataObject(double latitude, double longitude, QObject *parent =
nullptr);
void setLat(double latitude);
void setLon(double longitude);
double lat() const;
double lon() const;
double d_lat;
double d_lon;
}
为什么即使在调用 setContextProperty() 之后视图也看不到更新的模型?
感谢您的帮助
【问题讨论】: