我已经尝试解决这个问题2天了,我有一个gmaps项目,我使用以下代码将其集成到Qt项目中:
gmap locator;
ui->quickWidget->setSource(QUrl::fromLocalFile("/maps/main.qml"));
ui->quickWidget->rootContext()->setContextProperty("gmap",&locator);
ui->quickWidget->show();当我从MainWindow设置地图上的位置时,使用
locator.setData( "28.6082819", "77.0350079");但是,当我添加一个工具按钮并从它的插槽中执行相同的操作时,它就不起作用了,我已经将它连接到我的MainWindow中的一个自定义插槽,如下所示:
connect(ui->toolButton_5, SIGNAL(clicked()), this, SLOT(plotmap(QString(ui->lineEdit_3->text()),QString(ui->lineEdit_4->text()),&locator,&ui->quickWidget)));这是定制的插槽:
void MainWindow::plotmap(QString lat , QString lon, gmap *loc,QQuickWidget *view)
{
loc->setData("02.60","77.04");
view->show();
view->update();
}这是我的qml文件
gmap.cpp
#include "gmap.h"
gmap::gmap()
{
}
void gmap::setData(QString lat,QString lang)
{
qDebug(lat.toLatin1());
emit getLat(lat.toDouble());
emit getLang(lang.toDouble());
}gmap.h
#ifndef GMAP_H
#define GMAP_H
#include <QObject>
class gmap : public QObject
{
Q_OBJECT
public:
gmap();
signals:
void getLat(double lat);
void getLang(double lang);
public slots:
void setData(QString lat,QString lang);
};
#endif // GMAP_Hmain.qml
import QtQuick 2.6
import QtQuick.Window 2.2;
import QtPositioning 5.6;
import QtLocation 5.9
import Qt3D.Input 2.1
import QtQuick.Controls 2.2;
Window {
width: Qt.platform.os == "android" ? Screen.width : 512
height: Qt.platform.os == "android" ? Screen.height : 512
visible: true
Plugin {
id: mapPlugin
name: "osm"
PluginParameter {
name: 'osm.mapping.highdpi_tiles'
value: !!1 }
}
Connections{
target: gmap
onGetLat : mapmarker.center.latitude = lat
}
Connections{
target: gmap
onGetLang : mapmarker.center.longitude = lang
}
Connections{
target: gmap
onGetLang : map.center = QtPositioning.coordinate(mapmarker.center.latitude,mapmarker.center.longitude,150);
}
Map {
id: map
anchors.fill: parent
anchors.rightMargin: -15
anchors.bottomMargin: -10
anchors.leftMargin: 15
anchors.topMargin: 10
plugin: mapPlugin
center: QtPositioning.coordinate() // NSUT
zoomLevel: 14
activeMapType: supportedMapTypes[2]
Button {
x: 389
y: 445
text: "ADD MARKER"
anchors.right: parent.right
anchors.bottom: parent.bottom
anchors.bottomMargin: 27
anchors.rightMargin: 23
padding: 7
onClicked: gmap.setData(textField.text,textField1.text)
}
MapCircle {
id: mapmarker
center {
latitude: 28.6078
longitude: 77.0406
}
radius: 50.0
color: 'green'
border.width: 3
}
TextField {
id: textField
x: 176
y: 397
text: qsTr("")
anchors.right: parent.right
anchors.bottom: parent.bottom
anchors.bottomMargin: 75
anchors.rightMargin: 136
}
TextField {
id: textField1
x: 176
y: 445
anchors.right: parent.right
anchors.bottom: parent.bottom
anchors.bottomMargin: 27
anchors.rightMargin: 136
font.hintingPreference: Font.PreferDefaultHinting
}
}
}我的QQuickWidget中的地图不是update.What,我可能做错了,请告诉我。
发布于 2021-01-28 09:16:26
您的连接语法是错误的。
将locator作为成员存储在某个地方,例如在MainWindow中:
public:
void setLocator(gmap* loc) { m_locator = loc; }
private:
gmap* m_locator;向MainWindow添加一个插槽:
private slot:
plotmapFromLineEdits() { plotmap(ui->lineEdit_3->text(), ui->lineEdit_4->text(), m_locator, ui->quickWidget); }然后像这样把它连接起来:
connect(ui->toolButton_5, SIGNAL(clicked()), this, SLOT(plotmapFromLineEdits()));或者在C++11或更高版本中,您可以通过使用lambda来连接而无需定义插槽:
connect(ui->toolButton_5, SIGNAL(clicked()), [&](){plotmap(ui->lineEdit_3->text(), ui->lineEdit_4->text(), m_locator, ui->quickWidget);}));我不认为你每次都要show和update QQuickWidget。只需删除quickwidget参数即可。
最后,请确保类名以uppercase字母开头: gmap => GMap。
https://stackoverflow.com/questions/65933498
复制相似问题