Working example of ListView and FolderListModel? Or: what's wrong with this code?



  • I'm trying to implement a directory browser in QML. I found and read the examples that use FolderListModel. The model seems to work. However, the ListView doesn't respond to selection. Its currentIndex never changes. The behavior is the same on my N9 and on the emulator. I am perhaps overlooking something obvious. Can one of you please tell me what's wrong with this code?

    @
    // MainPage.qml
    import QtQuick 1.1
    import com.nokia.meego 1.0
    import Qt.labs.folderlistmodel 1.0

    Page {
    tools: commonTools

    ListView {
        id: listView
        anchors.fill: parent
        clip: true
        delegate: fileDelegate
        focus: true
        highlight: Rectangle { color: "lightsteelblue"; radius: 5 }
        model: folderModel
    
        FolderListModel {
             id: folderModel
             folder: "file:///"
             showDirs: true
             showDotAndDotDot: true
             sortField: "Name"
         }
    
         Component {
             id: fileDelegate
             Text {
                 font.family: "Nokia Pure Text"
                 font.pixelSize: 24
                 text: fileName
             }
         }
    
         onCurrentIndexChanged: {
             console.debug("listView currentIndex:", currentIndex)
         }
    }
    

    }
    @

    @
    // main.qml
    import QtQuick 1.1
    import com.nokia.meego 1.0

    PageStackWindow {
    id: appWindow

    initialPage: mainPage
    
    MainPage {
        id: mainPage
    }
    
    ToolBarLayout {
        id: commonTools
        visible: true
        ToolIcon {
            platformIconId: "toolbar-view-menu"
            anchors.right: (parent === undefined) ? undefined : parent.right
            onClicked: (myMenu.status == DialogStatus.Closed) ? myMenu.open() : myMenu.close()
        }
    }
    
    Menu {
        id: myMenu
        visualParent: pageStack
        MenuLayout {
            MenuItem { text: qsTr("Quit"); onClicked: Qt.quit() }
        }
    }
    

    }
    @



  • There is no built-in selection functional in ListView so you should implement it yourself. Just add MouseArea to your delegate and change ListView currentItem to index of the delegate in the onClicked handler.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.