I write a listview to show some information, when i clicked the button in the pDelegate
, the text on the button will change to "Added", now i want to save the model's data, when i restart the program, such "Added" items will autoshow "Added",but i can't realize this. when i mark a variable to the button text,all the button text will show "Added",i just want the specific item that matched the saved data to show "Added", Please help me, thanks!!!
ListModel{
id:pModel
}
ListView{
id:pView
anchors.fill:parent
model:pModel
delegate:pDelegate
anchors.margins: 15
Layout.alignment: Qt.AlignCenter
onAddChanged: {
console.log("added");
m_added = false;
}
}
Component{
id:pDelegate
Rectangle{
id:printerItem
width:parent.width
height:60
Text{
id:printerName
text:prname
font.pixelSize: 18
anchors.left: parent.left
anchors.leftMargin: pImg.width
anchors.verticalCenter: parent.verticalCenter
}
Component{
id:btnStyle3
ButtonStyle{
background: Rectangle{
width:control.width
height:control.height
color:printerItem.color
}
label:Text{
text:qsTr("Added")
font.pixelSize: 18
anchors.fill: parent
}
}
}
MouseArea{
id:itemMouseArea
hoverEnabled: true
anchors.fill: parent
onHoveredChanged: {
pView.currentIndex = index;
}
onEntered: {
printerItem.color = "#f5f5f5";
}
onExited: {
printerItem.color = "white";
}
}
Button{
id:btnAdd
width:60
height: 40
anchors.right: printerItem.right
anchors.verticalCenter: parent.verticalCenter
style:ButtonStyle{
id:btnAddStyle
background: Rectangle{
width:control.width
height:control.height
color: printerItem.color
}
label:Text{
id:btnText
color:control.hovered?"#0087ff":"black"
text:control.pressed?qsTr("Added"):qsTr("Add")
font.pixelSize: 18
anchors.fill: parent
}
}
onClicked: {
busyIndicator.visible = true;
busyIndicator.running = true;
clienter.setDefaultPrinter(printerName.text,index);
btnAdd.style = btnStyle3;
m_added = true;
}
Connections{
target:printerlist
onAddedChanged:{
console.log("onAddedChanged");
}
}
Connections{
target: printerlist
onStopSpinner:{
timer.start();
}
}
}
}
Change your buttons
Text
element propertytext
fromto
this will bind your buttons text value to value of
m_added
.and you can use the workerscript to
to solve this question.
I don't see where your
m_adde
is defined, but it looks like a single property or JavaScript variable not a role provided by your model.As a separate value needs to be associated with each entry in the model, the easiest option is to make "added" another bit of information provided by the model.
That way each delegate has its own state and that state can be saved and loaded as part of the model data.