QML 函数始终返回空白

QML function always returns blank

本文关键字:返回 空白 函数 QML      更新时间:2023-10-16

我用 QT 5.12 编写了一个C++的小程序。我的问题是我在 QML 文件中的 javascript 函数总是返回空白。我尝试使用 console.log() 来查看它是否真的到达我的函数,但命令调试窗口中没有写入任何内容。我确定我的 c++ 调用到达了该函数,因为如果我删除它,我会收到以下消息:
No such method QDeclarativeGeoMap::qmlFunction(QVariant) ,方法在调试器中的选定子项中也可见。

我得到的输出是:QML function returned: ""

有什么明显的原因我被退回空白吗?

更新的代码:
主窗口.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();
}

地图.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();
        }
    }
}

在尝试在 QML 中调用我的函数之前,我已经通过添加的代码解决了这个问题。

QObject* rt = ui->quickWidget->rootObject();
QObject* map = rt->children().at(1);