2015-10-04 38 views
0

如何從完整文件URL中提取文件baseName?QML:如何獲取文件baseName

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

    } 

fileUrl沒有像baseName我試圖谷歌搜索的特性,但沒有成功

+0

您可以從C++公開自定義類來QML與功能QFileInfo,因爲它不是從QObject派生的,並且不能直接暴露給QML。或者直接使用正則表達式,例如'fileUrl.toString()。replace(/ \\/g,'/')。replace(/.*\//,'')' – folibis

+0

[Get the path從QML URL](http://stackoverflow.com/questions/24927850/get-the-path-from-a-qml-url) – BaCaRoZzo

回答

1

你可以定義自己的basename功能

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


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