How to write conditional import statements in QML?

前端 未结 2 1127
执念已碎
执念已碎 2020-12-16 19:15

Like we have preprocessor directives in C++ for conditional includes.

Similarly, how to do conditional importing in QML?

if x  
    im         


        
相关标签:
2条回答
  • 2020-12-16 19:53

    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")
    }
    
    0 讨论(0)
  • 2020-12-16 20:08

    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"
    }
    
    0 讨论(0)
提交回复
热议问题