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. QML filename as a variable
Forum Updated to NodeBB v4.3 + New Features

QML filename as a variable

Scheduled Pinned Locked Moved Solved QML and Qt Quick
11 Posts 3 Posters 774 Views 3 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.
  • mzimmersM mzimmers

    Hi all -

    My app has a display with several "tiles." Each tile represents a piece of equipment. When a particular tile is clicked, I want to open a drawer and populate it with contents specific to that equipment item. My plan was to have each tile's drawer contents in a separate QML file.

    I'm having problems figuring out how to specify the QML filename programmatically. This works for desktop, but not on Android:

    QUrl::fromLocalFile("nga_demo/Pump.qml"), // m_drawerFile
    

    I suspect there's probably a better way of doing this anyway. Can someone provide some guidance on this?

    Thanks...

    piervalliP Offline
    piervalliP Offline
    piervalli
    wrote on last edited by
    #2

    @mzimmers

    Checks in the docs Loader

    mzimmersM 1 Reply Last reply
    0
    • piervalliP piervalli

      @mzimmers

      Checks in the docs Loader

      mzimmersM Offline
      mzimmersM Offline
      mzimmers
      wrote on last edited by
      #3

      @piervalli interesting idea. Here's a snippet of my tile.qml file:

      Item {
          id: tile
      
          property url drawerFile: "" // supplied by caller
          property bool drawerEnabled: true
      
          Rectangle {
              id: rect
              anchors.fill: parent
      
              MouseArea {
                  anchors.fill: parent
                  onClicked: {
                      console.log("drawerFile is " + tile.drawerFile)
                      if (drawerEnabled) {
                          drawer.enabled = true;
                          drawer.open();
                          drawerStack.push(tile.drawerFile)
                      } else {
                          drawerEnabled = null
                      }
                  }
              }
          }
      }
      

      I'm using a stack for the drawer contents.

      So, how would a loader figure into this? Do I need to remove the stack?

      Thanks...

      1 Reply Last reply
      0
      • mzimmersM mzimmers

        Hi all -

        My app has a display with several "tiles." Each tile represents a piece of equipment. When a particular tile is clicked, I want to open a drawer and populate it with contents specific to that equipment item. My plan was to have each tile's drawer contents in a separate QML file.

        I'm having problems figuring out how to specify the QML filename programmatically. This works for desktop, but not on Android:

        QUrl::fromLocalFile("nga_demo/Pump.qml"), // m_drawerFile
        

        I suspect there's probably a better way of doing this anyway. Can someone provide some guidance on this?

        Thanks...

        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #4

        @mzimmers said in QML filename as a variable:

        I'm having problems figuring out how to specify the QML filename programmatically. This works for desktop, but not on Android:

        QUrl::fromLocalFile("nga_demo/Pump.qml"), // m_drawerFile
        

        Android security policies place restrictions on getting absolute paths to files on disk.

        Why not embed the files in your app as a Qt Resource?

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        mzimmersM 1 Reply Last reply
        1
        • JKSHJ JKSH

          @mzimmers said in QML filename as a variable:

          I'm having problems figuring out how to specify the QML filename programmatically. This works for desktop, but not on Android:

          QUrl::fromLocalFile("nga_demo/Pump.qml"), // m_drawerFile
          

          Android security policies place restrictions on getting absolute paths to files on disk.

          Why not embed the files in your app as a Qt Resource?

          mzimmersM Offline
          mzimmersM Offline
          mzimmers
          wrote on last edited by
          #5

          @JKSH I tried this:

          qrc.PNG
          And in the code:

          QUrl::fromLocalFile("qrc:/Pump.qml"), // m_drawerFile
          

          But I get an error:

          QML StackView: push: file:///C:/Users/Michael.Zimmers/Qt_projects/build-nga_demo-Desktop_Qt_6_4_2_MinGW_64_bit-Release/qrc:/Pump.qml:-1 No such file or directory
          

          So the app is still looking in the build directory for the file. Am I not forming the qml entry properly?

          Thanks...

          JKSHJ 1 Reply Last reply
          0
          • mzimmersM mzimmers

            @JKSH I tried this:

            qrc.PNG
            And in the code:

            QUrl::fromLocalFile("qrc:/Pump.qml"), // m_drawerFile
            

            But I get an error:

            QML StackView: push: file:///C:/Users/Michael.Zimmers/Qt_projects/build-nga_demo-Desktop_Qt_6_4_2_MinGW_64_bit-Release/qrc:/Pump.qml:-1 No such file or directory
            

            So the app is still looking in the build directory for the file. Am I not forming the qml entry properly?

            Thanks...

            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #6

            @mzimmers said in QML filename as a variable:

            QUrl::fromLocalFile("qrc:/Pump.qml")

            It's not a a local file, it's a resource file: QUrl("qrc:/Pump.qml")

            In your screenshot, you can also right-click "Pump.qml" and click "Copy URL"

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            mzimmersM 1 Reply Last reply
            2
            • JKSHJ JKSH

              @mzimmers said in QML filename as a variable:

              QUrl::fromLocalFile("qrc:/Pump.qml")

              It's not a a local file, it's a resource file: QUrl("qrc:/Pump.qml")

              In your screenshot, you can also right-click "Pump.qml" and click "Copy URL"

              mzimmersM Offline
              mzimmersM Offline
              mzimmers
              wrote on last edited by
              #7

              @JKSH oops...thanks for catching that silliness on my part. (BTW: the entry copies to the clipboard as "://Pump.qml" for some reason; not surprisingly, the program doesn't like that.)

              This fixed the immediate issue, though any use of other application .qml files inside Pump causes a similar problem, so I guess I need to embed all the files into the app. This would seem to be a good time to ask again, am I doing this the right way, or is there a preferred method I'm not aware of?

              Thanks...

              piervalliP JKSHJ 2 Replies Last reply
              0
              • mzimmersM mzimmers

                @JKSH oops...thanks for catching that silliness on my part. (BTW: the entry copies to the clipboard as "://Pump.qml" for some reason; not surprisingly, the program doesn't like that.)

                This fixed the immediate issue, though any use of other application .qml files inside Pump causes a similar problem, so I guess I need to embed all the files into the app. This would seem to be a good time to ask again, am I doing this the right way, or is there a preferred method I'm not aware of?

                Thanks...

                piervalliP Offline
                piervalliP Offline
                piervalli
                wrote on last edited by piervalli
                #8

                @mzimmers,
                For Android the best mode is the resources.
                On windows instead to improve the time to development you can read the qml file on hardisk.

                I have created a global var prefixQml that in Android I'd qrc instead on windows the qml folder
                PS
                (This is my opinion)

                mzimmersM 1 Reply Last reply
                0
                • piervalliP piervalli

                  @mzimmers,
                  For Android the best mode is the resources.
                  On windows instead to improve the time to development you can read the qml file on hardisk.

                  I have created a global var prefixQml that in Android I'd qrc instead on windows the qml folder
                  PS
                  (This is my opinion)

                  mzimmersM Offline
                  mzimmersM Offline
                  mzimmers
                  wrote on last edited by
                  #9

                  @piervalli when you say "to improve the time to development," are you referring to the additional step of adding files to the qml.qrc file?

                  piervalliP 1 Reply Last reply
                  0
                  • mzimmersM mzimmers

                    @piervalli when you say "to improve the time to development," are you referring to the additional step of adding files to the qml.qrc file?

                    piervalliP Offline
                    piervalliP Offline
                    piervalli
                    wrote on last edited by
                    #10

                    @mzimmers
                    When files are as resources every changed is builded, if there are many qml files the time to build could be little height.

                    1 Reply Last reply
                    0
                    • mzimmersM mzimmers has marked this topic as solved on
                    • mzimmersM mzimmers

                      @JKSH oops...thanks for catching that silliness on my part. (BTW: the entry copies to the clipboard as "://Pump.qml" for some reason; not surprisingly, the program doesn't like that.)

                      This fixed the immediate issue, though any use of other application .qml files inside Pump causes a similar problem, so I guess I need to embed all the files into the app. This would seem to be a good time to ask again, am I doing this the right way, or is there a preferred method I'm not aware of?

                      Thanks...

                      JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on last edited by
                      #11

                      @mzimmers said in QML filename as a variable:

                      the entry copies to the clipboard as "://Pump.qml" for some reason

                      Sounds like you clicked "Copy path" instead of "Copy URL"

                      I guess I need to embed all the files into the app. This would seem to be a good time to ask again, am I doing this the right way, or is there a preferred method I'm not aware of?

                      The best practice is to always embed QML files as resources, instead of having your application load and run files from disk. The latter makes it very easy for someone to accidentally (or maliciously) change the program.

                      If you're using Qt 6, qt_add_qml_module() takes care of resource embedding for you (plus it gives the additional benefit of compiling your QML code into C++ where it can): https://forum.qt.io/topic/141780/reopened-using-subdirectories-in-a-cmake-project

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      1 Reply Last reply
                      0

                      • Login

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