简体   繁体   中英

How to use internal QML types from external QML files?

In a qrc based qml project, I have the Internal.qml which I can use decalratively in the Internal {} format.

I also have some external / remote (arbitrary location) External.qml which also needs to be able to use Internal.qml in the declarative Internal {} format.

However, when I try to load External.qml , it errors out that Internal is not a type .

I tried adding qrc:/ to the import paths, but it doesn't seem to work, neither do the several import directive hacks I tried.

It doesn't seem unreasonable to expect that when the application loads an external QML file, that should be able to use the internally available QML types. What import directive do I need to use to get this to run?

You can use a directory import statement (it can be an absolute path)

If your Internal.qml path is qrc:/internal/Internal.qml you can do the following in External.qml :

import "qrc:/internal"

Internal {}

Alternatively you could define your own module with a qmldir file and do a normal module import.

Create a qrc:/internal/qmldir file with this content:

module internal
Internal 1.0 Internal.qml

You then need to add qrc:/ to your import paths (when importing a module, the QML engine tries to open <import-path>/<module-name>/qmldir for each of the import path in its list).

After that, you can do this from External.qml :

import internal 1.0

Internal {}

One solution, albeit somewhat sub-optimal, is to use QQmlComponent :

  QQmlComponent c(engine);
  c.setData(readInTheRemoteQML, QUrl("qrc:/Test"));

The "qrc:/Test" part does allow the external file to properly resolve the internal types.

If url is provided, it is used to set the component name and to provide a base path for items resolved by this component.

This does the trick, but I am still open to a more "organic" solution.

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