简体   繁体   中英

How to include qml file from js file

I collect some examples of import statement in QML. But one is lacking — import a qml file from js file.

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?

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:

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

in QML you would write something like this:

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

var lib = LibraryFactory.initLibrary(JsQtTest)

lib.foo();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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