我写了一个listview来显示一些信息,当我点击pDelegate
中的按钮时,按钮上的文本会变成“Added”,现在我想保存模型的数据,当我重新启动程序时,这样“Added “项目会自动显示”添加“,但我无法意识到这一点。当我将一个变量标记为按钮文本时,所有按钮文本都会显示“已添加”,我只希望与保存的数据匹配的特定项目显示“已添加”,请帮助我,谢谢!如何在不点击listview委托的情况下更改按钮的文本?
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();
}
}
}
}
请将代码缩减为[最小工作示例](http://stackoverflow.com/help/mcve)。只需删除'Image'和'BusyIndicator'等不必要的项目等。 – folibis
AFAIK QML不支持写入磁盘。你需要在C++中实现一个模型,为你做持久化的东西。 – derM
是的,我将数据(字符串)保存在C++中,并使用JavaScript全局变量来接收字符串,该字符串是一个项目名称列表,我将用它显示在该监听上,然后我将使用该模型来追加元素。但是当按钮文本已经显示时,如何更改我想在委托中更改的按钮文本,我无法使用按钮ID访问按钮文本,更改按钮文本的唯一方法是单击按钮,所以如何改变它不点击按钮? –