我用QT 5.12用C ++编写了一个小程序。我的问题是QML文件中的javascript函数始终返回空白。我尝试使用console.log()
来查看它是否确实达到了我的功能,但是在命令调试窗口中什么也没写。
我确定我的c ++调用会到达该函数,因为如果删除它,则会收到以下消息:
No such method QDeclarativeGeoMap::qmlFunction(QVariant)
,该方法在调试器中的选定子项中也可见。
我得到的输出是:QML function returned: ""
有什么明显的原因让我退回空白吗?
更新的代码:
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->txtLatitude->setText("45.5075693");
ui->txtLongtitude->setText("13.5824982");
ui->lblStatus->setText("Ready!");
QQmlEngine engine;
QQmlComponent component(&engine,QUrl(QStringLiteral("qrc:/Map.qml")));
ui->quickWidget->setSource(QStringLiteral("qrc:/Map.qml"));
object = component.create();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_btnSimulate_clicked()
{
QTimer *timer;
if(ui->btnSimulate->text() == "Simulate")
{
ui->btnSimulate->setText("End simulation");
ui->txtLatitude->setEnabled(false);
ui->txtLongtitude->setEnabled(false);
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(UpdateCoordinates()));
timer->start(1000);
}
else {
ui->btnSimulate->setText("Simulate");
ui->txtLatitude->setEnabled(false);
ui->txtLongtitude->setEnabled(false);
}
}
void MainWindow::UpdateCoordinates()
{
float LATDEGM = (60 * 1853.181);
float DEG2RAD = (PI / 180.0);
QString speedData = ui->sboxSpeed->text();
bool ok;
AirSpeed = speedData.toInt(&ok);
AirCourse = (AirCourse + 360) % 360;
Dx = AirSpeed * sin((float)AirCourse * DEG2RAD);
Dy = AirSpeed * cos((float)AirCourse * DEG2RAD);
QString dat = ui->txtLatitude->text();
Lat = dat.toDouble();
QString dat2 = ui->txtLongtitude->text();
Lon = dat2.toDouble();
Dx /= 3.6;
Dy /= 3.6;
Lat += Dy / LATDEGM;
Lon += Dx / (LATDEGM * cos(Lat * DEG2RAD));
ui->txtLatitude->setText(QString::number(Lat));
ui->txtLongtitude->setText(QString::number(Lon));
QObject* map = object->children().first();
QVariant returnedValue;
QVariant msg = "Hello from C++";
QMetaObject::invokeMethod(map, "qmlFunction",
Q_RETURN_ARG(QVariant, returnedValue),
Q_ARG(QVariant, msg));
qDebug() << "QML function returned:" << returnedValue.toString();
}
Map.qml
import QtQuick 2.0
import QtQuick.Window 2.0
import QtLocation 5.6
import QtPositioning 5.6
Item{
id: itemControl
objectName: "itemControl"
width: 512
height: 512
visible: true
property int maxZoom: 15
property int minZoom: 15
property real newLat: 45.5075693
property real newLon: 13.5824982
Plugin {
id: mapPlugin
name: "esri"
}
Map {
id:map
objectName: "map"
anchors.fill: parent
maximumZoomLevel: itemControl.maxZoom
minimumZoomLevel: itemControl.minZoom
width: 512
height: 512
plugin: mapPlugin
center {
latitude: itemControl.newLat
longitude: itemControl.newLon
}
zoomLevel: 15
function startupFunction(){
console.log("Finish method");
}
function qmlFunction(msg) {
console.log("Got message:", msg);
return "some return value";
}
Component.onCompleted: {
startupFunction();
}
}
}
答案 0 :(得分:0)
在尝试在QML中调用函数之前,我已经通过添加以下代码解决了该问题。
QObject* rt = ui->quickWidget->rootObject();
QObject* map = rt->children().at(1);