代码之家  ›  专栏  ›  技术社区  ›  teukkam

如何在QML中进行基于状态的模型更改

  •  1
  • teukkam  · 技术社区  · 14 年前

    我正在尝试制作一个基于QML的字典应用程序。它通过XMLRESTfulAPI获取单词定义,并在ListView中显示它们。我让它在这种基本模式下工作。但是现在我尝试为ListView实现两种状态:带有定义的标准视图和搜索失败时的“DodYouMean”类型建议列表。

    我当前的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

    2 回复  |  直到 14 年前
        1
  •  2
  •   MartinJ    14 年前

    SuggestionDelegate正在实例化。delegate属性需要一个组件,它将为它显示的每个项实例化它自己。因此,要提供组件而不是实例,需要将SuggestionDelegate包装到组件中,并在PropertyChanges中使用组件id:

    Component {
        id: suggestionDelegate
        SuggestionDelegate { }
    }
    
        2
  •  0
  •   teukkam    14 年前

    虽然Martin的解决方案解决了我遇到的问题,但我还是为UI想出了一个更好的设计。由于definitions和suggestions视图是互斥的,所以我将它们实现为自己的项,它们具有相同的几何图形,并且根据当前状态显示或隐藏。这也允许很好的过渡动画。