2017-03-01 38 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變量,而不是你的模型提供的角色。作爲一個單獨的值需要與模型中的每個條目相關聯,最簡單的選擇是「添加」模型提供的另一部分信息。

這樣每個委託都有自己的狀態,並且該狀態可以作爲模型數據的一部分保存和加載。

相關問題