如何从 QML 中的列表视图访问 currentItem 的角色? [英] How do you access the roles of the currentItem from a listview in QML?

查看:13
本文介绍了如何从 QML 中的列表视图访问 currentItem 的角色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 QML 中的 ListView 访问角色.基本上,我的 QML 中有这个:

I'm trying to access a role from a ListView in QML. Essentially, I have this in my QML:

ListView {
    id: myId
    model: myModel
    delegate: Item {
        Text {
            text: model.text
        }
        Text {
            text: model.moreText
        }
    }
}

myModel 是一个 QAbstractListModel 实现.其中的 QML 部分是一个可重用的组件,因此该模型可以具有具有各种数据类型的任意数量的不同角色.我想做的是绑定到 ListView 的 currentItem 属性的给定角色的值.换句话说,我希望页面上有一些其他的 Component 可以将属性绑定到 ListView 中当前选定的项目,如下所示:

myModel is a QAbstractListModel implementation. The QML portion of this is a reusable component, so the model could have any number of different roles with various data types. What I would like to do is bind to the value of a given role of the currentItem property of the ListView. In other words, I'd like to have some other Component on the page that could bind a property to the currently selected item in the ListView as follows:

Text {
    text: myId.currentItem.text // Or myId.currentItem.model.text (or something similar)
}

请记住,我需要这个通用可用的,因为我会为许多模型类型做很多这样的事情,并且我试图不为每个模型和 ListView 编写那种自定义代码.

Please keep in mind that I need this generically available, as I'll be doing this a lot for a number of model types and I'm trying not to write that kind of custom code for each model and ListView.

似乎访问当前选定项目的属性应该很简单,但据我所知这是不可能的.当只有一个角色时,模型似乎受到不同的对待,这一事实使问题变得更加复杂.我的意思是有时您通过 model.roleName 访问您的角色,而当只有一个角色时,您使用 modelData.

It seems like it should be simple to access a property of the currently selected item, but as far as I can tell it is not possible. The problem is complicated further by the fact that models appear to be treated differently when there is only one role. By this I mean that sometimes you access your roles via model.roleName whereas when there is only one role you use modelData.

如果有人有任何建议,我将不胜感激.非常感谢!

If anybody has any suggestions, I would truly appreciate it. Thanks so much!

编辑

我发现了这个:

http://comments.gmane.org/gmane.comp.lib.qt.qml/1778

但是,这似乎对我不起作用.当我尝试在 QML 脚本中使用数据时遇到类型错误,并且没有可用的类型转换,所以我不知道该怎么做.欢迎提出任何建议!

However, this doesn't appear to work for me. I'm getting type errors when I try to use the data in my QML scripts, and there is no type casting available so I'm not sure what to do. Any suggestions are welcome!

谢谢!

杰克

推荐答案

代码在http://comments.gmane.org/gmane.comp.lib.qt.qml/1778 应该可以工作,尽管如果属性名为数据",我确实会看到错误;看起来它正在覆盖一些现有的内置属性.将其重命名为myData"似乎可行:

The code at http://comments.gmane.org/gmane.comp.lib.qt.qml/1778 should work, although I do see errors if the property is named 'data'; it looks like it is overriding some existing built-in property. Renaming it to 'myData' seems to work:

ListView {
    id: myId
    model: myModel
    delegate: Item {
        property variant myData: model
        Text {
            text: model.text
        }
        Text {
            text: model.moreText
        }    
    }
}

Text { text: myId.currentItem.myData.text }

(原始帖子中的 myId.currentItem.text 代码不起作用,因为它试图引用您的委托中的 text 属性,但没有'不存在.)

(The myId.currentItem.text code in the original post didn't work because this was trying to refer to a text property within your delegate, which didn't exist.)

关于在委托中引用 modelmodelData,差异取决于模型的类型,而不是模型中角色的数量.如果模型是字符串列表或对象列表,则 modelData 用于引用委托中的单个字符串或对象(因为字符串列表和对象列表没有任何作用).对于所有其他模型,包括 QML ListModel 和 Qt C++ QAbstractItemModel,model.role 可用于引用委托中的角色.

In regards to referring to model vs modelData within the delegate, the difference depends on the type of the model, rather than the number of roles in the model. If the model is a string list or object list, modelData is used to refer to the individual string or object from within a delegate (since string lists and object lists do not have any roles). For all other models, including the QML ListModel and the Qt C++ QAbstractItemModel, model.role can be used to refer to a role within a delegate.

这篇关于如何从 QML 中的列表视图访问 currentItem 的角色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆