ListView Select item based on some value



  • Hello i want to be able to select a item based on a stored database value, when the ListView is first loaded
    Let's say i have something like this:
    @
    ListView {
    id: myListView
    anchors.fill: parent

    model: SomePythonModel
    delegate: Component {
    Text {
    id: Title
    text: model.list.title
    }

    Text {
      id: uniqueID
      text: model.list.uniqueID
      visible: false
    }
    

    }
    @

    My sulotion was to loop throu the items onCompleted and check if uniqueID == dbValue, like so
    @
    for(var i=0; i<myListView.contentItem.children.length; i++) {
    var ID = myListView.contentItem.children[i].uniqueID;
    if(ID == wrapper.dbValue) {
    selectedIndex = i;
    break;
    }
    }
    @

    But that only works if the item in question is one of the items that is visible on load.
    Does anybody have an idée on how to achive this?



  • You can iterate on the myListView.model.



  • [quote author="dmcr" date="1352705848"]You can iterate on the myListView.model.[/quote]

    I have tried to do something like this but with no success, and i havent found any information to help me out.

    coud you provide a link or write a short example of iterating on myListView.model?



  • -You can iterate on the myListView.model.-
    Well it's not so simple, however.
    You can, but it depends on how you define your model.
    It works with QDeclarativeListProperty, otherwise you had to add some code like in this "link":http://doc.qt.nokia.com/4.7-snapshot/qdeclarativemodels.html#qabstractitemmodel as you may already know.

    You can be inspired as well with this "monologer":http://qt-project.org/forums/viewthread/21787/ :)



  • Thanks for your answers but i can't find anything in those links that will help me with iterating the model how ever monologer had a link to "Chris' Blog":http://cdumez.blogspot.se/2010/11/how-to-use-c-list-model-in-qml.html that defines a find function but no exampel use of that function, and i havent been able to find anymore, and i get function undefind when i tried to implement it. On the other hand i cant call any other function in the model so i guess they are not ment to be accessed that way.

    here is an example of how my list model in python
    @
    class MyListModel(QtCore.QAbstractListModel):
    COLUMNS = ('item',)

    def __init__(self, items):
        QtCore.QAbstractListModel.__init__(self)
        self.items = items
        self.setRoleNames(dict(enumerate(myListModel.COLUMNS)))
    
    def rowCount(self, parent=QtCore.QModelIndex()):
        return len(self.items)
    
    def data(self, index, role):
        if index.isValid() and role == UserListModel.COLUMNS.index('item'):
            return self.items[index.row()]
        return None
    
    # This is newly implemented
    def find(self, title):
        return 1
        #for i in range(len(self.items)):
        #    if self.items[i].getTitle() == title:
        #        return i
        #return -1
    

    @

    and it is exposed to qml with: self.rc.setContextProperty('SomePythonModel', MyListModel(itemsArray))



  • Hello,

    I cannot help you really in python, just two things:

    • If you add Q_INVOKABLE before the declaration of the rowCount() function for example, then you can access in javascript to the value.
    • In your delegate, you can have for example @isLocalSelectedIndex = uniqueID == wrapper.dbValue /an outside value you can access/
      @

    and then

    @onIsLocalSelectedIndexChanged :
    if( isLocalSelectedIndex )
    { you can change the global value you want}@

    So globally you can almost do everything you want with the model you have right now



  • Thanks so much for all your help, but in the end it was my stupid brain that couldn't see the simplest solution, instead of using selectedIndex to determinate the state I'm now using selectedUniqueID.

    it came to me in a dream... for real =)

    So for people with the same train wreck of a brain as me, I will give an example:
    @
    Page {
    alias string selectedUniqueID: ""

    ListView {
    id: myListView
    anchors.fill: parent

    model: SomePythonModel
    delegate: Component {
      Text {
        id: Title
        text: model.list.title
      }
    
      Text {
        id: uniqueID
        text: model.list.uniqueID
        visible: false
      }
    
      MouseArea {
        onClicked: selectedUniqueID = uniqueID.text
      }
      states: State {
        when: selectedUniqueID == uniqueID.text
        ...
      }
    }
    

    }

    Component.onCompleted: {
    selectedUniqueID = wrapper.dbValue
    }
    }
    @



  • Don't know who wrote :"the best pieces i code i am proud of are not the ones i made in front of my computer, but under my shower!" :)


Log in to reply
 

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