QML filename as a variable
-
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...
-
@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...
-
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...
@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?
-
@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?
@JKSH I tried this:
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...
-
@JKSH I tried this:
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...
@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"
-
@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"
@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...
-
@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...
@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) -
@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)@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?
-
@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?
-
M mzimmers has marked this topic as solved on
-
@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...
@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