PDA

View Full Version : QML FolderListModel how to set nameFilters for directory with Script



fngost
19th February 2017, 07:27
I want to create a filter to display the directories. The script works well with files, but does not work with directories. What am I doing wrong? :confused:



function updateFilter()
{
var text = searchField.text
var filter = "*"
for(var i = 0; i<text.length; i++)
filter+= text[i]
filter+="*"
print(filter)
folderDirModel.nameFilters = [filter]
}

TextField{
id: searchField
width: parent.width
font.pointSize: 16
padding: 0
visible: true
onTextChanged: updateFilter()
}
ListView {
id: dirView
anchors.topMargin: 50
anchors.leftMargin: 75
antialiasing: false
scale: 1
spacing: 1
anchors.fill: parent
clip: true
model: folderDirModel
delegate: folderDeligate

FolderListModel {
id: folderDirModel
objectName: "folderDirModel"
sortField: FolderListModel.Name
showDirs: true
showFiles: false
folder: "file:///Users/public/Directory/"
nameFilters: ["*.*"]
}

anda_skoa
19th February 2017, 12:36
Can you modify the example to be actually runnable, e.g. with qmlscene?

Your function looks curious, especially the loop. Why not concatenate the whole string instead of character by character?


var filter = "*" + text + "*"


Cheers,
_

fngost
19th February 2017, 15:15
Well, here's the scheme. var filter = "*" + text + "*" has not given result...



import QtQuick 2.5
import QtQuick.Window 2.0
import QtQuick.Controls 2.0
import QtQuick.Dialogs 1.2
import Qt.labs.folderlistmodel 2.1

ApplicationWindow
{
id: app
width: 800
height: 600

property var directoryModel: folderDirModel
property var directoryListView: karDirView

TextField{
id: searchField
x: 0
width: parent.width
font.pointSize: 16
padding: 0
visible: true
onTextChanged: updateFilter()
}

ListView {

id: karDirView
anchors.topMargin: 50
anchors.leftMargin: 75
antialiasing: false
scale: 1
spacing: 1
anchors.fill: parent
clip: true
model: folderDirModel
delegate: folderDeligate

FolderListModel {
id: folderDirModel
objectName: "folderDirModel"
sortField: FolderListModel.Name
showDirs: true
showFiles: false
folder: "file:///Users/public/Directory/"
nameFilters: ["*.*"]


}


Component{
id: folderDeligate
Row {
Rectangle {
id: directoryRectangle
width: 400
height: 50
gradient: Gradient {
GradientStop { position: 0.0; color: "#c8892c" }
GradientStop { position: 1.0; color: "#ebb07c" }
}
Text {
text: fileName
anchors.leftMargin: 10
anchors.verticalCenter: parent.verticalCenter
color: "#192437"
font.pointSize: 16
}

}
}

}

}


function updateFilter()
{
var text = searchField.text
var filter = "*" + text + "*"
//for(var i = 0; i<text.length; i++)
// filter+= text[i]
// filter+="*"
print(filter)
folderDirModel.nameFilters = [filter]
}
}

anda_skoa
21st February 2017, 10:26
Thanks for the runnable example.

Meanwhile I found this in the documentation for the FolderListModel (http://doc.qt.io/qt-5/qml-qt-labs-folderlistmodel-folderlistmodel.html):


Note: Directories are not excluded by filters.

and


Note that the nameFilters are not applied to directories.


Cheers,
_

fngost
23rd February 2017, 05:34
Bad news. Well I will look for another solution. Thank you