2010-09-22 100 views
1

我正在嘗試製作基於QML的字典應用程序。它通過XML RESTful API獲取單詞定義並將它們顯示在ListView中。我有它在這種基本模式下工作。但現在我試圖爲ListView:標準視圖實現兩種狀態,其中包含定義,並且在搜索失敗時顯示「您的意思是」類型建議列表。如何在QML中創建基於狀態的模型更改

我爲ListView當前的代碼是這樣的:

ListView 
{ 
    SuggestionModel{id:suggestionModel; currentWord : "test"} 
    SuggestionDelegate{id:suggestionDelegate} 
    model : XmlModel{id: standardModel; currentWord : "test"} 
    delegate : ListDelegate{id:standardDelegate} 
    clip : true 
    anchors.top : hbox.bottom 
    y : hbox.height + 3 
    width : parent.width 
    height : parent.height - hbox.height 
     id : list 
     states : 
       State { name: "suggestion"; when: list.model == suggestionModel || 
         list.model.status == XmlListModel.Ready && list.count == 0 
       PropertyChanges { 
        target: list 
        model : suggestionModel 
        delegate : suggestionDelegate 
       } 
      } 

     focus : true 
     keyNavigationWraps : true 
    } 

這給這個錯誤:

Unable to assign QObject* to QDeclarativeComponent* 

PropertyChanges聲明。還有一個綁定循環,但這不是我無法修復的問題。我的問題是如何定義狀態。我不能在State聲明中實例化模型和委託,因爲解釋器會抱怨創建一個特定於狀態的對象。

回答

2

SuggestionDelegate正在被實例化。委託屬性需要一個Component,它將爲它顯示的每個項目實例化自己。因此,要提供一個組件,而不是一個實例你需要用的SuggestionDelegate在組件和使用組件ID中的PropertyChanges:

Component { 
    id: suggestionDelegate 
    SuggestionDelegate { } 
} 
+0

謝謝,這工作完美! – teukkam 2010-09-23 11:54:17

0

雖然馬丁的解決方案固定我是有這個問題,我想出了一個更好的爲UI設計。由於定義和建議視圖是相互排斥的,因此我將它們各自實現爲具有相同幾何形狀並根據當前狀態顯示或隱藏的項目。這也允許很好的過渡動畫。

相關問題