如何在 QML 中进行基于状态的模型更改
我正在尝试制作一个基于 QML 的字典应用程序。它通过 XML RESTful API 获取单词定义并将其显示在 ListView 中。我让它在这种基本模式下工作。但现在我正在尝试为 ListView 实现两种状态:带有定义的标准视图和搜索失败时的“您的意思是”类型建议列表。
我当前的 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
声明, 。还有一个绑定循环,但这并不是我无法解决的问题。我的问题是如何定义状态。我无法在状态声明中实例化模型和委托,因为解释器会抱怨创建特定于状态的对象。
I'm trying to make a QML-based dictionary application. It fetches the word definition via an XML RESTful API and displays them in a ListView. I have it working in this rudimentary mode. But now I'm trying to implement two states for the ListView: standard view with the definitions and a "did you mean" type suggestions list for when the search failed.
My current code for the ListView is like this:
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
}
which gives this error:
Unable to assign QObject* to QDeclarativeComponent*
for the PropertyChanges
declaration. There is also a binding loop but that's not really an issue I couldn't fix. My problem is how do I define the states. I can't instantiate the model and delegate inside the State declaration either as the interpreter will complain about creating a state-specific object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正在实例化 SuggestionDelegate。 delegate 属性需要一个 Component,它将为其显示的每个项目实例化自身。因此,要提供组件而不是实例,您需要将 SuggestionDelegate 包装在组件中,并在 PropertyChanges 中使用组件 id:
SuggestionDelegate is being instantiated. The delegate property requires a Component which it will instantiate itself for each item it displays. So to provide a Component rather than an instance you need to wrap the SuggestionDelegate in a Component and use the Component id in the PropertyChanges:
尽管 Martin 的解决方案解决了我遇到的问题,但我提出了更好的 UI 设计。由于定义和建议视图是互斥的,因此我将每个视图实现为自己的项目,它们具有相同的几何形状,并根据当前状态显示或隐藏。这也可以实现漂亮的过渡动画。
Though Martin's solution fixed the problem I was having, I came up with a better design for the UI. Since the definitions and suggestions view are mutually exclusive, I implemented each as its own item which have the same geometry and are displayed or hidden according to the current state. This also allows for nice transition animations.