how can i change the text of the button without cl

2019-06-14 19:21发布

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


                }
}

标签: qml
3条回答
Root(大扎)
2楼-- · 2019-06-14 19:41

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.

查看更多
\"骚年 ilove
3楼-- · 2019-06-14 19:43
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.

查看更多
We Are One
4楼-- · 2019-06-14 19:44

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.

查看更多
登录 后发表回答