我正在Java脚本中创建QT对象&从JavaScript发送信号到QML Slot。
我的工作环境: Qt 5.8 MSVC2015 64位,Windows 7 64位。
代码testJava.js:
var internalQmlObject = Qt.createQmlObject('import QtQuick 2.5; \
QtObject { \
signal someSignal(int value) \
}',
Qt.application,
'InternalQmlObject');
function runNow(){
internalQmlObject.someSignal(42);
}
QML代码:
import QtQuick 2.5
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
import QtWebEngine 1.4
import "testJava.js" as Helper
ApplicationWindow {
width: 1024
height: 750
visible: true
WebEngineView {
id: webEngine
anchors.fill: parent
url: "qrc:/TestPage.html"
function someSlot(v) {
console.log("Signal received " + v);
}
onLoadingChanged: {
Helper.internalQmlObject.someSignal.connect(someSlot);
}
}
}
但Java Script无法创建QtQuick 2.5对象:
启动D:\ task \ QT_Test \ build-testURL-Desktop_Qt_5_8_0_MSVC2015_64bit-Debug \ debug \ testURL.exe ...
js:未捕获的ReferenceError:未定义Qt
[4492:5796:0427/113341:INFO:CONSOLE(1)] " Uncaught ReferenceError:Qt未定义",source:qrc:/testJava.js(1)
任何有关JavaScript无法识别QT的建议?
答案 0 :(得分:0)
我找到了解决方法,创建了javascript文件&按如下所示加载对象:
将“testJava.js”导入为Helper
但它不应该是QML的理想解决方案。