QML 函数始终返回空白
QML function always returns blank
我用 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);
相关文章:
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 什么时候在C++中返回常量引用是个好主意
- 你能重载对象变量名本身返回的内容吗
- 为什么 Serial.println(<char[]>);返回随机字符?
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何获取std::result_of函数的返回类型
- TCP 服务器/客户端:客户端 recv() 返回空白缓冲区
- QML 函数始终返回空白
- 字符串:: substr()返回一个空白字符串
- 返回空白
- QT 5.4 QPrinterInfo::printerName返回空白
- OpenCV VideoCaptures 有时会返回空白帧
- Qt5.4 QOpenGLFramebufferObject toImage 返回空白图像
- 为什么从文件输入中读取 std::string 会返回空白
- WASAPI GetDisplayName 返回空白
- Getline只返回空白
- 我在我的程序中添加了一个void函数,它不接受任何参数,现在我的程序在运行时只返回一个空白终端
- ReadFile()返回一个空白数据
- QtCreator QMediaPlayer元数据返回空白QString
- 为什么这段代码在试图将.txt放入向量并对其进行cout时会返回空白控制台