[Solved]Button update, ListView and pyside model
-
Edit:I changed the Title to suit the subject better.
Hi
I am trying to update the selected item in a ListView with some new data from pyside.I have made it to work when i click on an item I update that item with new data from python. But It will not work when I try to do it from a button, why?
Here I show you what works and further down I show you the button I need help with.
@ Column {
id:colSum
Text { x:15; text: '<b>Sum:</b> ' + model.zenItem.sum
MouseArea {
anchors.fill: parent
onClicked: { controller.setSum(model.zenItem)}} } }@
the snipet is from a Component that is shown in this ListView:
@ListView {
id: pythonList
highlight: highlightBar // current Item
highlightFollowsCurrentItem: true//false
model: pythonListModel
focus: true
keyNavigationWraps: false // index går runt
spacing:5delegate: gameList
}@
Now beneath the list i have a button:
@ Rectangle{
id:btnSetSum
anchors.top:pythonList.bottom
width:90; height:25; x:210; y:10
border.width:2
border.color:"blue"
color:"lightblue"
Text{
id:txtSetSum
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
text: "Set Sum"
}MouseArea{
id:mouseSetSum
anchors.fill:parent
onClicked:{ controller.setSum(pythonListModel.zenItem)}
}}@
in python when i just try to print it out to see what I got it prints out None. Is it because when I click my button there is now item in my ListView that is selected?
How do I send the selected item to my python script from my button? -
I have made it to work, I don't know if there is an easier way of doing it but this is the way I have done it...
I changed the button like this (pythonList is the id of the ListView.)
@onClicked:{ controller.rollDice(pythonList.currentIndex)}@in python controller class i did this:
@QtCore.Slot(int)
def rollDice(self,currentindex):
wrappedPlayers[currentindex].setDice()@and in the wrapper I did this:
@def setDice(self):
tmp=random.randint(1,6)
print tmp
self._player['dice']=tmp
self.changed.emit()
changed = QtCore.Signal()
dice = QtCore.Property(int, _dice,setDice, notify=changed)
#(_dice is the function that just returns the name not used now.)
@this is what i base my list on:
@players = [{'name':'Jerry','sum':24,'dice':1},{'name':'Jonas','sum':35,'dice':6},{'name':'Ted','sum':29,'dice':4},{'name':'Jimmy','sum':25,'dice':3}]wrappedPlayers = [Wrapper(player) for player in players]
playerList = playerListModel(wrappedPlayers)
...
rc.setContextProperty('pythonListModel', playerList)@