3

How I can extract file baseName from full file URL?

FileDialog
    {
        id: fileDialog
        title: "Oooopen"
        onAccepted:
        {
        console.log(fileUrl)    
        }

    }

fileUrl do not have properties like baseName I tried to googling, but without success

Dmitry Bubnenkov
  • 9,415
  • 19
  • 85
  • 145
  • You can expose custom class from C++ to QML with functionality of QFileInfo since it isn't derived from QObject and cannot be exposed to QML directly. Or just do it with Regexp, for example `fileUrl.toString().replace(/\\/g,'/').replace(/.*\//, '')` – folibis Oct 04 '15 at 23:18
  • Possible duplicate of [Get the path from a QML url](http://stackoverflow.com/questions/24927850/get-the-path-from-a-qml-url) – BaCaRoZzo Oct 05 '15 at 00:06

1 Answers1

5

You could define your own basename function

function basename(str)
{
    return (str.slice(str.lastIndexOf("/")+1))
}


FileDialog
{
    id: fileDialog
    title: "Oooopen"
    onAccepted:
    {
        console.log(basename(fileUrl.toString()))    
    }
}
  • 1
    In Qt 5.10 (may be in early version too) this function looks like: `return (String(str).slice(String(str).lastIndexOf("/")+1))` – eska2000 Jun 29 '18 at 11:44