QML-导入外部JavaScript文件 [英] QML - Import external JavaScript file

查看:644
本文介绍了QML-导入外部JavaScript文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以像这样导入已经在项目树中的JavaScript文件:

I can import JavaScript files which are already part of the project tree like this:

import "myFile.js" as MyFile

对于我的项目中尚未包含的外部文件,是否有任何方法可以做到这一点,即通过将绝对或相对路径传递到光盘上的文件?

Is there any way to do this for external files that aren't already included in my project, i.e. by passing an absolute or relative path to the file on my disc?

推荐答案

对于某些问题,例如:

是否可以执行类似[this ...]

Is it possible to do something like [this...]

通常最简单的方法就是尝试一下.

usually the easiest way, is to try it out.

您的问题中缺少重要的细节:

In your question a important detail is missing:

所讨论的QML文件是否在qrc文件中?

如果是,那么您需要告诉QML,它应该在qrc之外.与图片一样,您可以通过在其前面加上file:///来实现此目的.

If it is, then you need to tell QML that it shall look outside the qrc. As with pictures, you do that, by prefixing it with file:///.

绝对路径在这里可以正常工作.亲戚比较棘手,因为您需要预测要从哪个目录进入.我不能告诉你.

A absolute path works fine here. The relative is tricky, as you need to predict from which directory you are coming. I can't tell you that.

如果QML不在qrc中,则无论如何都将在文件系统上指定相对路径,因此这里没有问题.您甚至不需要在file:///

If the QML is not in a qrc, than you will specify a relative path on the file system in any case, so no problems here. You don't even need to prepend the file:///

如果您想让它更远程一些,请从互联网上尝试一下:

If you want to have it a litte more remote, try it from the internet:

import QtQuick 2.5
import QtQuick.Controls 2.0
import 'http://code.qt.io/cgit/qt/qtdeclarative.git/plain/examples/quick/demos/photoviewer/PhotoViewerCore/script/script.js' as Test

ApplicationWindow {
    id: window
    visible: true
    width: 600
    height: 600

    Button {
        text: 'Calculate Scale of an Image: 100x100px for a target size of 200px'
        onClicked: console.log('It is:', Test.calculateScale(100, 100, 200) + '!\nMagical!')
    }
}

要获得更动态的导入,您可以创建不超过此内容的代理脚本:

For a more dynamic import, you can create a proxy script with no more than this content:

//proxyScript.js

function include(path) { Qt.include(path) }

然后您可以按以下方式在QML文件中使用它:

Then you can use it in your QML file as this:

import QtQuick 2.0
import QtQuick.Controls 2.0
import 'proxyScript.js' as Script1
import 'proxyScript.js' as Script2

ApplicationWindow {
    Component.onCompleted {
        // Load scripts here
        var path1 = [...] // Build the absolute path of some script to load
        var path2 = [...] // Build the absolute path of another script to load
        Script1.include(path1) // Now you can access the content of the script at path1 via `Script1.functionFromPath1...`
        Script2.include(path2)
    }
    [...]
}

您还可以在一个proxyScript中导入多个.js文件.但是,导入的脚本的功能将位于相同的名称空间中.

You can also import multiple .js-files in one proxyScript. The functions of the scripts you import however will then be in the same name space.

当然,如果需要,您还可以具有更多的静态代理脚本:

Of course you can also have more static proxy scripts if you want:

//staticProxyScript.js

Qt.include('file:/My/Absolute/Path/To/A/Script/That/I/Want/To/Use.js')

这篇关于QML-导入外部JavaScript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆