Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Conditional Import
Forum Updated to NodeBB v4.3 + New Features

Conditional Import

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
10 Posts 6 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rhb327
    wrote on last edited by
    #1

    I'm trying to use loader to do something like:

    import yada 1.0
    import whatever 2.0
    
    Loader {
        source:  condition ? "import.qml" : ""
    }
    
    Rectangle {
    ...
    

    where import.qml contains only:

    import MyQml 1.0
    

    I'm receiving a expected token 'numeric literal' syntax error. Any ideas to resolve this error or a conditional import example to share?

    Thanks,
    -Rich

    eyllanescE 1 Reply Last reply
    0
    • R rhb327

      I'm trying to use loader to do something like:

      import yada 1.0
      import whatever 2.0
      
      Loader {
          source:  condition ? "import.qml" : ""
      }
      
      Rectangle {
      ...
      

      where import.qml contains only:

      import MyQml 1.0
      

      I'm receiving a expected token 'numeric literal' syntax error. Any ideas to resolve this error or a conditional import example to share?

      Thanks,
      -Rich

      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by eyllanesc
      #2

      @rhb327 Please put the real code and not pseudo code, also indicate what the .qml names are. For example in your first code there are 2 roots(Loader and Rectangle). In problems where a syntax error is pointed out then the real code is necessary.

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      1 Reply Last reply
      0
      • IntruderExcluderI Offline
        IntruderExcluderI Offline
        IntruderExcluder
        wrote on last edited by
        #3

        QML is declarative, you can't do such things. If you want to achieve some dynamic behaviour for your scene you can use Loader or Component.
        You can also try qmlRegisterModule which allows imports without any registered types inside.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          rhb327
          wrote on last edited by
          #4

          Actual source snippets...

          import QtQuick 2.0
          import MyController 1.0
          
          Loader {
              source: !RUN_ON_WINDOWS ? "wifi_single.qml" : ""
          }
          
          CustomController{
              property bool fwUpdtProgress: false
              property bool autoFwUpdate: false
          ...
          

          And wifi_single.qml:

          import Wifi.Singleton 1.0
          
          

          RUN_ON_WINDOWS is defined in another module as:

          #ifdef Q_OS_WIN
              viewer.rootContext()->setContextProperty("RUN_ON_WINDOWS", QVariant(true));
          #else
              viewer.rootContext()->setContextProperty("RUN_ON_WINDOWS", QVariant(false));
          #endif
          

          Error is:

          wifi_single.qml:2: error: Expected token `numeric literal'
          

          Thanks,
          Rich

          raven-worxR 1 Reply Last reply
          0
          • R rhb327

            Actual source snippets...

            import QtQuick 2.0
            import MyController 1.0
            
            Loader {
                source: !RUN_ON_WINDOWS ? "wifi_single.qml" : ""
            }
            
            CustomController{
                property bool fwUpdtProgress: false
                property bool autoFwUpdate: false
            ...
            

            And wifi_single.qml:

            import Wifi.Singleton 1.0
            
            

            RUN_ON_WINDOWS is defined in another module as:

            #ifdef Q_OS_WIN
                viewer.rootContext()->setContextProperty("RUN_ON_WINDOWS", QVariant(true));
            #else
                viewer.rootContext()->setContextProperty("RUN_ON_WINDOWS", QVariant(false));
            #endif
            

            Error is:

            wifi_single.qml:2: error: Expected token `numeric literal'
            

            Thanks,
            Rich

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            @rhb327
            better use the following in QML:

            Qt.platform.os === "windows"
            

            And wifi_single.qml:

            import Wifi.Singleton 1.0

            so your "wifi_single.qml" just contains the import statement?!

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • R Offline
              R Offline
              rhb327
              wrote on last edited by
              #6

              I don't seem to be able to use if(Qt.platform.os === "windows") for conditional imports. This only works in code regions from what I can tell.

              if(RUN_ON_WINDOWS) also works in code regions but not for conditional imports.

              Thanks,
              -Rich

              raven-worxR 1 Reply Last reply
              0
              • R rhb327

                I don't seem to be able to use if(Qt.platform.os === "windows") for conditional imports. This only works in code regions from what I can tell.

                if(RUN_ON_WINDOWS) also works in code regions but not for conditional imports.

                Thanks,
                -Rich

                raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #7

                @rhb327
                i proposed this to use instead of your context property approach.
                conditional imports are jsut not possible in QML.

                You can use Loader to load components/QML-files which use 2 different imports.

                But to me it rather seems that your issue lies somewhere else. Why do you need 2 different imports for different platforms in the first place?

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                C 1 Reply Last reply
                0
                • raven-worxR raven-worx

                  @rhb327
                  i proposed this to use instead of your context property approach.
                  conditional imports are jsut not possible in QML.

                  You can use Loader to load components/QML-files which use 2 different imports.

                  But to me it rather seems that your issue lies somewhere else. Why do you need 2 different imports for different platforms in the first place?

                  C Offline
                  C Offline
                  caicx
                  wrote on last edited by
                  #8

                  @raven-worx

                  import QtGraphicalEffects 1.0 // Required by Qt 5.x.x
                  vs
                  import Qt5Compat.GraphicalEffects // Required by Qt 6.x.x

                  raven-worxR 1 Reply Last reply
                  0
                  • fcarneyF Offline
                    fcarneyF Offline
                    fcarney
                    wrote on last edited by
                    #9
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • C caicx

                      @raven-worx

                      import QtGraphicalEffects 1.0 // Required by Qt 5.x.x
                      vs
                      import Qt5Compat.GraphicalEffects // Required by Qt 6.x.x

                      raven-worxR Offline
                      raven-worxR Offline
                      raven-worx
                      Moderators
                      wrote on last edited by
                      #10

                      @caicx said in Conditional Import:

                      import QtGraphicalEffects 1.0 // Required by Qt 5.x.x
                      vs
                      import Qt5Compat.GraphicalEffects // Required by Qt 6.x.x

                      yes and?
                      Since the major version of Qt has changed you should also advance with your project, instead of importing a compat module of a previous version for the whole lifespan of Qt 6.
                      The compat module is intended to be used as a quick solution until the porting of your application to Qt6 has finished

                      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                      If you have a question please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply
                      1

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved