我正在Java腳本中創建QT對象&從JavaScript發出信號到QML插槽。JavaScript未能創建QT對象
我的工作環境: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腳本未能創建QtQuick 2.5對象:
Starting D:\task\QT_Test\build-testURL-Desktop_Qt_5_8_0_MSVC2015_64bit-Debug\debug\testURL.exe...
js: Uncaught ReferenceError: Qt is not defined
[4492:5796:0427/113341:INFO:CONSOLE(1)] "Uncaught ReferenceError: Qt is not defined", source: qrc:/testJava.js (1)
任何建議,爲什麼失敗的JavaScript識別QT?
我測試了代碼並沒有任何問題。你可以試試我的[code](https://github.com/eyllanesc/stackoverflow/tree/master/JSTestingQt)。 – eyllanesc
我無法用'qt5.7.1/qtquick2.7'確認這種行爲 – derM
我發現這個問題,在HTML頁面中,我加載testJava.js,導致這個問題:
– Sandy