QML 函数总是返回空白

托马兹

我用 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();
}

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从QML返回代码退出

来自分类Dev

从Python调用QML函数

来自分类Dev

无法调用 QML 函数

来自分类Dev

Google App脚本函数返回空白

来自分类Dev

QML无法识别C ++函数

来自分类Dev

QML无法识别C ++函数

来自分类Dev

QML 连接信号到函数

来自分类Dev

QML ReferenceError:<something>不是从qml文件调用的Js函数定义的

来自分类Dev

Vim函数展开总是返回空字符串

来自分类Dev

为什么SQL函数总是返回空结果集?

来自分类Dev

Vim函数expand总是返回空字符串

来自分类Dev

为什么SQL函数总是返回空结果集?

来自分类Dev

ImageButton返回空白

来自分类Dev

postgreSQL返回空白

来自分类Dev

节点总是返回空数组

来自分类Dev

JsonEncoder 总是返回空数组

来自分类Dev

bsearch() 总是返回空指针

来自分类Dev

方法总是返回空列表

来自分类Dev

Jsonpath 总是返回空列表

来自分类Dev

使用QML调用虚拟C ++函数

来自分类Dev

QMetaObject :: invokeMethod无法调用QML / JS函数

来自分类Dev

QML:Lambda函数意外起作用

来自分类Dev

函数运行时的QML加载视图

来自分类Dev

如何从另一个QML文件调用QML文件中定义的Javascript函数?

来自分类Dev

在QML中的TabView内的另一个QML文件中调用函数或属性

来自分类Dev

如何从另一个QML文件调用QML文件中定义的Javascript函数?

来自分类Dev

使用另一个QML对象作为参数从C ++调用QML函数

来自分类Dev

QML ReferenceError:<something>不是从QML文件中调用的Js函数定义的

来自分类Dev

Laravel getRelations()返回空白