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

查看:1067
本文介绍了你如何在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 属性的给定角色的价值。换句话说,我想有一些其他的组件,可以在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)
}

请记住,我需要这个一般可用,我会做这个有很多为一些模型类型的,我尽量不写那种定制code对每种型号和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.

如果任何人有任何建议,我将真正AP preciate它。非常感谢!

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

修改

我发现这一点:

<一个href=\"http://comments.gmane.org/gmane.comp.lib.qt.qml/1778\">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!

谢谢!

杰克

推荐答案

在code。在<一个href=\"http://comments.gmane.org/gmane.comp.lib.qt.qml/1778\">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 的在原岗位code没有工作,因为这是试图引用的文本的财产委托的范围内,这是不存在的。)

(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.)

在问候委托内参照的模型 VS modelData 的,所不同的依赖于模型的类型,而不是在模型角色的数量。如果该模型是一个字符串列表或对象列表, 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天全站免登陆