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

发布于 2024-09-24 16:59:13 字数 1244 浏览 9 评论 0原文

我正在尝试制作一个基于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与往事干杯 2024-10-01 16:59:13

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

Component {
    id: suggestionDelegate
    SuggestionDelegate { }
}

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:

Component {
    id: suggestionDelegate
    SuggestionDelegate { }
}
乄_柒ぐ汐 2024-10-01 16:59:13

尽管 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文