Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to get current file name with extension?
Qt 6.12 LTS is out! See what's new in the release blog

How to get current file name with extension?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 132 Views 2 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.
  • L Offline
    L Offline
    Leooooooooo
    wrote last edited by
    #1

    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

    1 Reply Last reply
    0
    • KH-219DesignK Offline
      KH-219DesignK Offline
      KH-219Design
      wrote last edited by
      #2

      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:28
      

      The older forum thread has additional ideas for parsing the string if you prefer to see only a substring of what is in Error().stack

      or here is how it looks when the QML file is compiled into the C++ app binary:

      expression for myCustomPropertyName@qrc:/qml/VersionLabel.qml:28
      

      www.219design.com
      Software | Electrical | Mechanical | Product Design

      L 1 Reply Last reply
      1
      • KH-219DesignK KH-219Design

        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:28
        

        The older forum thread has additional ideas for parsing the string if you prefer to see only a substring of what is in Error().stack

        or here is how it looks when the QML file is compiled into the C++ app binary:

        expression for myCustomPropertyName@qrc:/qml/VersionLabel.qml:28
        
        L Offline
        L Offline
        Leooooooooo
        wrote last edited by
        #3

        @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:28
        

        The older forum thread has additional ideas for parsing the string if you prefer to see only a substring of what is in Error().stack

        or here is how it looks when the QML file is compiled into the C++ app binary:

        expression for myCustomPropertyName@qrc:/qml/VersionLabel.qml:28
        

        Took 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:103

        So I'd guess I'd need to some how parse the first line of the stack

        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
        • Unsolved