How to get current file name with extension?
-
Does QML have a way to retrieve the current file's name? For example in Python I can simply do "file" and this would get me the name. I've tried Qt.resolvedUrl("") but it just gives undefined
-
From this older post: https://forum.qt.io/topic/122818/get-file-name-line-number-and-function-name-in-qml-javascript-how
I learned that this is possible in QML:
const e = new Error() console.log(e.stack)Sample output:
expression for myCustomPropertyName@file:///long/path/to/my/qml/VersionLabel.qml:28The older forum thread has additional ideas for parsing the string if you prefer to see only a substring of what is in
Error().stackor here is how it looks when the QML file is compiled into the C++ app binary:
expression for myCustomPropertyName@qrc:/qml/VersionLabel.qml:28 -
From this older post: https://forum.qt.io/topic/122818/get-file-name-line-number-and-function-name-in-qml-javascript-how
I learned that this is possible in QML:
const e = new Error() console.log(e.stack)Sample output:
expression for myCustomPropertyName@file:///long/path/to/my/qml/VersionLabel.qml:28The older forum thread has additional ideas for parsing the string if you prefer to see only a substring of what is in
Error().stackor here is how it looks when the QML file is compiled into the C++ app binary:
expression for myCustomPropertyName@qrc:/qml/VersionLabel.qml:28@KH-219Design said in How to get current file name with extension?:
From this older post: https://forum.qt.io/topic/122818/get-file-name-line-number-and-function-name-in-qml-javascript-how
I learned that this is possible in QML:
const e = new Error() console.log(e.stack)Sample output:
expression for myCustomPropertyName@file:///long/path/to/my/qml/VersionLabel.qml:28The older forum thread has additional ideas for parsing the string if you prefer to see only a substring of what is in
Error().stackor here is how it looks when the QML file is compiled into the C++ app binary:
expression for myCustomPropertyName@qrc:/qml/VersionLabel.qml:28Took a look but it seems that if you have a file that does this and is called by another QML file it'll stack the expression to that file as well. For example if I call it from a file called Page.qml but the error stack is called from Settings.qml I'd get
Debug: expression for onCompleted@file:///Settings.qml:14
expression for onCompleted@file:///Page.qml:103So I'd guess I'd need to some how parse the first line of the stack