How to write conditional import statements in QML?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 04:08:09

Depending on what you want to achieve, a possible workaround is to use a Loader. But it does not import a module, it just allows to choose dynamically which QML component you'll use.

Loader
{
    source: condition?"RedRectangle.qml":"BlueRectangle.qml"
}

extending a little bit the @Yoann answer:

 Loader
 {
        source: x?"ABC.qml":"PQR.qml"
 }

where ABC.qml :

import ABC 1.0
...

and PQR.qml :

import PQR 2.0  
...

or if don't what to have real qml files you can create them at runtime with:

Loader{
  source:x ? Qt.createQmlObject('import ABC 1.0;',parentItem,"dynamicSnippet1") : Qt.createQmlObject('import PQR 2.0;',parentItem,"dynamicSnippet1")
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!