嗨,我最近与QT一起玩移动开发。我开始简单地使用C 中的所有代码(考虑到我有更多的经验(,现在我开始使用QML。在我的应用程序中,我在UI中有一个Qquickwidget,其中显示了地图。每次发射坐标时,目标都可以简单地将地图集中。这就是我设置QML
的方式ui->quickWidget->setSource(QUrl("qrc:/geom_map.qml"));
这是我的QML文件
Rectangle {
width: 300
height: 300
visible: true
Plugin {
id: osmPlugin
name: "esri"
// specify plugin parameters if necessary
// PluginParameter {
// name:
// value:
// }
}
Map {
id: map
anchors.fill: parent
plugin: osmPlugin
center: QtPositioning.coordinate(51.0, -114.0)
zoomLevel: 10
activeMapType: map.supportedMapTypes[1] // This selects the type of map to display
}
我声明了一个类,以处理将坐标发送到QML并在QML上设置连接以接收信号
#ifndef COORDUPDATE_H
#define COORDUPDATE_H
#include <QObject>
#include <QVariant>
class coordUpdate : public QObject
{
Q_OBJECT
public:
explicit coordUpdate(QObject *parent = nullptr);
// Q_INVOKABLE void sendupd(double xcoord, double ycoord);
private:
signals:
void sendCoord(QVariant xcoord,QVariant ycoord);
private slots:
public slots:
void sendupd(double xcoord, double ycoord);
};
#endif // COORDUPDATE_H
#include "coordupdate.h"
#include <QDebug>
coordUpdate::coordUpdate(QObject *parent) :
QObject(parent)
{
}
void coordUpdate::sendupd(double xcoord,double ycoord){
emit sendCoord(xcoord,ycoord);
qDebug() << "signal emitted";
}
我遇到的问题是,如果我打电话
void MainWindow::setGPSLocation(QGeoPositionInfo geoPositionInfo)
{
statusBar()->showMessage("Updating position...",1000);
QString text="Location=unknown";
if (geoPositionInfo.isValid())
{
// get the current location coordinates
QGeoCoordinate geoCoordinate = geoPositionInfo.coordinate();
// transform coordinates to lat/lon
qreal latitude = geoCoordinate.latitude();
qreal longitude = geoCoordinate.longitude();
qreal altitude = geoCoordinate.altitude();
double lon = longitude;
double lat = latitude;
//on position updated we also need to record the data
coordUpdate upd;
ui->quickWidget->rootContext()->setContextProperty("updcoord",&upd);
ui->quickWidget->setSource(QUrl("qrc:/geom_map.qml"));
upd.sendupd(longitude,latitude);
qDebug() << "reading coordinates" << longitude << latitude;
}
}
然后,坐标会更新,但整个过程都得到了刷新。是否有更好的方法更新发送到QML文件的信息?我知道我会在QML中更好地开发QML,但我暂时对使用Qquickwiget非常感兴趣。
我不问如何将信号从C 连接到QML。代码这样做。我问的是如何通过通过信息正确更新地图。我已经阅读了文档。不幸的是,没有太多的文档专注于qquickwidgets,而是与qqmlengine实例化的主类。
如下所述P>
- 将您的C 类公开为QML(属性,信号,插槽..(,并将其注册为可启用的QML类型
- 与1相同,但将其注册为单顿类型,因此它是由QML引擎实例化的,而不是在QML代码中明确的
- 与1相同,但可以通过上下文属性向QML提供,例如,在这种情况下,您可以在应用程序的C 侧创建类的实例
通过移动地图内的连接并在mainwindow.cpp和mainwindow.h中重新定义QML实例化的方式来解决它。这样,每次QML捕获信号时,地图都会正确刷新。
添加到mainwindow.cpp
private:
coordUpdate *upd;
更改
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->startU->setDisabled(true);
setupLogging();
setupGPS();
coordUpdate upd;
ui->quickWidget->rootContext()->setContextProperty("updcoord",upd);
ui->quickWidget->setSource(QUrl("qrc:/geom_map.qml"));
}
to
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->startU->setDisabled(true);
setupLogging();
setupGPS();
upd = new coordUpdate;
ui->quickWidget->rootContext()->setContextProperty("updcoord",upd);
ui->quickWidget->setSource(QUrl("qrc:/geom_map.qml"));
}
和qml
Rectangle {
width: 300
height: 300
visible: true
Plugin {
id: osmPlugin
name: "esri"
}
Map {
id: map
anchors.fill: parent
plugin: osmPlugin
// center: QtPositioning.coordinate(y, x)
zoomLevel: 15
activeMapType: map.supportedMapTypes[1] // This selects thetype of mapa to display
MapCircle {
id:circle
center: QtPositioning.coordinate(y, x)
radius: 50
color: 'red'
}
// Create connections with c++
Connections // Define actions for custom slots
{
id:cppConnection
target: updcoord
onSendCoord: {
// To access signal parameter, name the parameter
console.log("signal received",ycoord,xcoord)
circle.center = QtPositioning.coordinate(ycoord,xcoord)
}
}
}
}