我一直在试图解决这个问题2天了。我有一个gmaps QML项目,我使用以下代码将其集成到我的Qt widgets项目中:
gmap locator;
ui->quickWidget->setSource(QUrl::fromLocalFile("/maps/main.qml"));
ui->quickWidget->rootContext()->setContextProperty("gmap",&locator);
ui->quickWidget->show();
当我使用
从主窗口设置地图上的位置时locator.setData( "28.6082819", "77.0350079");
它按预期工作,然而,当我添加一个工具按钮并从它的插槽做同样的事情时,它不起作用。我将它连接到主窗口中的自定义插槽,如下所示:
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_H
main.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中的地图没有更新。我哪里做错了,请告诉我。
您的connect语法错误。
将locator
存储为成员,例如:
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