2017-03-01 110 views
-1

我写了一个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(); 
         } 
        } 


       } 
} 
+0

请将代码缩减为[最小工作示例](http://stackoverflow.com/help/mcve)。只需删除'Image'和'BusyIndi​​cator'等不必要的项目等。 – folibis

+0

AFAIK QML不支持写入磁盘。你需要在C++中实现一个模型,为你做持久化的东西。 – derM

+0

是的,我将数据(字符串)保存在C++中,并使用JavaScript全局变量来接收字符串,该字符串是一个项目名称列表,我将用它显示在该监听上,然后我将使用该模型来追加元素。但是当按钮文本已经显示时,如何更改我想在委托中更改的按钮文本,我无法使用按钮ID访问按钮文本,更改按钮文本的唯一方法是单击按钮,所以如何改变它不点击按钮? –

回答

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

,你可以使用workerscript到

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

来解决这个问题。

0

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

更改按钮Text元素属性text

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

这将您的按钮文本值绑定到的m_added价值。

+0

我已经尝试过了,但是当m_added改变时,所有的按钮文本都会改变,我只想要一个按钮,我已经保存了改变的结果。 –

+0

@poolooloowu,我看,但看起来像你的按钮获取一些变量的价值,而不是模型数据的角色。也许尝试从'ListView.onAddChanged'中删除'm_added = false'?它看起来像你正试图设置模型角色的价值,在那里你不能访问模型数据和意外声明新变量。 – Solant

+0

最后,我添加模型角色pModel.append({“prname”:pname [i],“prstate”:qsTr(Jsclient.pstate)}); –

0

我看不到你的m_adde被定义在哪里,但它看起来像一个属性或JavaScript变量,而不是你的模型提供的角色。作为一个单独的值需要与模型中的每个条目相关联,最简单的选择是“添加”模型提供的另一部分信息。

这样每个委托都有自己的状态,并且该状态可以作为模型数据的一部分保存和加载。

相关问题