简体   繁体   English

如何从js文件包含qml文件

[英]How to include qml file from js file

I collect some examples of import statement in QML. 我收集了一些 QML中的import语句示例 But one is lacking — import a qml file from js file. 但是缺少一个-从js文件导入qml文件。

Documentation says that it is a way to do it 文档这是一种方法

.import Qt.test 1.0 as JsQtTest

but I can't make it work. 但我不能使它工作。 I appreciate suggestions on my example collection and the answer to the question — how to import qml file from js file? 我感谢我的示例集合中的建议以及问题的答案-如何从js文件导入qml文件?

One possible workaround for the issue I think is to do not import Qt library inside JS file but instead pass it as a parameter to the function exported from JS file: 我认为该问题的一种可能的解决方法是,不要在JS文件中导入Qt库,而是将其作为参数传递给从JS文件导出的函数:

function initLibrary(JsQtTest) {
   return {
      foo: function() {
         // use JsQtTest here
      }
   };
}

in QML you would write something like this: 在QML中,您将编写如下内容:

import "myLibrary.js" as LibraryFactory
import Qt.test 1.0 as JsQtTest
...

var lib = LibraryFactory.initLibrary(JsQtTest)

lib.foo();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM