c++ - QML How to dynamically access ListView items -
i have listview has dynamically added items, , kinda want know how access items example index. specifically, want have color of item rectangle change when change color using color dialog. guessed should maybe possible first set variable current item before calling color dialog , in onaccepted method change color of item using variable, don't know how achieve of in qml, because rather new qml. maybe can offer cleaner way change color of item's rectangle when color dialog closed (onaccepted). thx help! :)
import qtquick 2.0 import qtquick.window 2.2 import qtquick.controls 1.4 import qtquick.dialogs 1.2 import qtquick.controls.styles 1.4 rectangle { id: listviewcontainer width: parent.width/10*9; height: 50 behavior on height { numberanimation { duration: 100; } } gradient: gradient { gradientstop {position: 0.0; color: "white" } gradientstop {position: 1.0; color: "silver" } } radius: 5 colordialog { id: colordialog title: "please choose color" onaccepted: { console.log("you chose: " + colordialog.color) qt.quit() } onrejected: { console.log("canceled") qt.quit() } } component { id: playerdelegate item { anchors.left: parent.left anchors.right: parent.right height: 50 column { text { text: '<b>name:</b> ' + name } row { mousearea { width: 20 height: 20 onclicked: { colordialog.visible = true; playercolor = colordialog.color; //open color dialog } rectangle { radius: 3 anchors.fill: parent color: playercolor } } } } } } listview { id: playerlistview anchors.fill: parent model: listmodel { id: playerlistviewmodel; listelement { name: "bill smith" playercolor: "red" } } delegate: playerdelegate } button { id: addplayerbutton anchors.top: playerlistview.bottom anchors.left: playerlistview.left anchors.right: playerlistview.right style: buttonstyle { label: text { text: "add new player" horizontalalignment: text.center } } onclicked: { root.addnewplayer(playerlistview); //dont worry method playerlistviewmodel.append({name: "billy", playercolor: "blue"}); listviewcontainer.height += 50; } } }
here sure fire way make working colordialog --
in playerdelegate
component { id: playerdelegate item { anchors.left: parent.left anchors.right: parent.right height: 50 column { text { text: '<b>name:</b> ' + name } /* object store value color selector */ item { id: colorselector property color color: "#000000" oncolorchanged: { playercolor = color; } } /* box display color */ rectangle { //layout.fillwidth: true height: 120 width: 120 anchors.left: button.right //layout.alignment: qt.alignhcenter color: colorselector.color border.width: 1 border.color: "#000000" } /* button open dialog -- can mousearea or other clickable object */ button { id: button text: "browse..." onclicked: { colordialog.color = colorselector.color colordialog.open() } } /* actual color dialog delegate */ colordialog { id: colordialog modality: qt.applicationmodal title: "please choose color" onaccepted: colorselector.color = currentcolor } } } }
Comments
Post a Comment