How to use JavaScript libraries on QML(如何在 QML 上使用 JavaScript 库)
问题描述
我在 5.12.2 上使用了一些带有 QML 的 javascript 库.其中一些像 Proj4JS 一样工作.但是在将 geographiclib.js 库与 QML 一起使用时出现错误.JavaScript库如何导入QML?
I use some javascript libraries with QML on 5.12.2. Some of them works like Proj4JS. But I get errors when using geographiclib.js library with QML. How can the JavaScript library be imported into QML?
main.qml:
import QtQuick 2.12
import QtQuick.Window 2.12
import "geographiclib.js" as MyGeo
Window {
visible: true
width: 640
height: 480
Component.onCompleted: {
var Geodesic = MyGeo.GeographicLib.Geodesic,
DMS = MyGeo.GeographicLib.DMS,
geod = Geodesic.WGS84;
var r = geod.Inverse(23, 22, 44, 29);
console.log("distance is: ", r.s12.toFixed(3) + " m")
}
}
错误:
qrc:/geographiclib.js:3081: ReferenceError: window is not defined
qrc:/main.qml:9: TypeError: Cannot read property 'Geodesic' of undefined
推荐答案
最简单的方法是让 GeographicLib
在全球范围内可用:
The easiest way to do this, is to make GeographicLib
available globally:
在geographiclib.js文件的最后,更改
window.GeographicLib = geo;
到
this.GeographicLib = geo;
然后你就可以使用:
main.qml:
import QtQuick 2.12
import QtQuick.Window 2.12
import "geographiclib.js" as ThenItWillBeAvailableGlobally
Window {
visible: true
width: 640
height: 480
Component.onCompleted: {
var Geodesic = GeographicLib.Geodesic,
DMS = GeographicLib.DMS,
geod = Geodesic.WGS84;
var r = geod.Inverse(23, 22, 44, 29);
console.log("distance is: ", JSON.stringify(r))
}
}
导致:
qml: distance is: {"lat1":23,"lat2":44,"lon1":22,"lon2":29,"a12":21.754466225665134,"s12":2416081.7576307985,"azi1":13.736139413215236,"azi2":17.669059640534535}
<小时>
如果您根本不想更改 geographiclib.js 文件,您可以添加一个全局窗口对象,例如:
It you don't want to change the geographiclib.js file at all, you could add a global window object using, for example:
window.js:
this.window = this;
然后使用:
import QtQuick 2.12
import QtQuick.Window 2.12
import "window.js" as ThenWindowWillBeAvailableGlobally
import "geographiclib.js" as ThenGeographicLibWillBeAvailableGlobally
Window {
visible: true
width: 640
height: 480
Component.onCompleted: {
var Geodesic = GeographicLib.Geodesic,
DMS = GeographicLib.DMS,
geod = Geodesic.WGS84;
var r = geod.Inverse(23, 22, 44, 29);
console.log("distance is: ", JSON.stringify(r))
}
}
<小时>
如果您不想添加任何全局变量但您很乐意编辑 geographiclib.js 文件,那么您只需将第 68 行移至文件顶部:
If you don't want to add any global variables but you are happy to edit the geographiclib.js file, then you could just move line 68 to the top of the file:
var GeographicLib = {};
/*
* Geodesic routines from GeographicLib translated to JavaScript. See
* https://geographiclib.sourceforge.io/html/js/
并在文件更改结束时
} else {
/******** otherwise just pollute our global namespace ********/
window.GeographicLib = geo;
}
});
到
} else if (typeof window === 'object') {
/******** otherwise just pollute our global namespace ********/
window.GeographicLib = geo;
}
});
然后你的 main.qml 就可以正常工作了.
and then your main.qml will work just fine.
这篇关于如何在 QML 上使用 JavaScript 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 QML 上使用 JavaScript 库
- 400或500级别的HTTP响应 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Fetch API 如何获取响应体? 2022-01-01