如何在 QML 上使用 JavaScript 库

2022-01-19 00:00:00 qt 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.

相关文章