Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved]Button update, ListView and pyside model
Forum Updated to NodeBB v4.3 + New Features

[Solved]Button update, ListView and pyside model

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 1 Posters 3.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BadOmen
    wrote on last edited by
    #1

    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:5

     delegate: 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?

    1 Reply Last reply
    0
    • B Offline
      B Offline
      BadOmen
      wrote on last edited by
      #2

      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)@

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved