how can i change the text of the button without clicked in the listview delegate?

烂漫一生 提交于 2019-12-23 06:09:24

问题


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();
                        }
                    }


                }
}

回答1:


Change your buttons Text element property text from

text:control.pressed?qsTr("Added"):qsTr("Add")

to

text: m_added ? qsTr("Added") : qsTr("Add")

this will bind your buttons text value to value of m_added.




回答2:


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.




回答3:


finally,i add a model role 
pModel.append({"prname":pname[i],"prstate": qsTr(Jsclient.pstate)}); 

and you can use the workerscript to

sendmessage({"prname":pname[i],"prstate": qsTr(Jsclient.pstate)}) 

to solve this question.



来源:https://stackoverflow.com/questions/42521956/how-can-i-change-the-text-of-the-button-without-clicked-in-the-listview-delegate

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!