如何从 QML 中的列表视图访问 currentItem 的角色?

发布于 2024-10-20 23:37:03 字数 1152 浏览 1 评论 0原文

我正在尝试从 QML 中的 ListView 访问角色。本质上,我的 QML 中有这样的内容:

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

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

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

请记住,我需要这个通用可用,因为我将对多种模型类型进行大量操作,并且我尝试不为每个模型和 ListView 编写此类自定义代码。

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

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

编辑

我发现了这个:

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

但是,这似乎对我不起作用。当我尝试在 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 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)
}

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.

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!

EDIT

I found this:

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

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!

Thanks!

Jack

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

や莫失莫忘 2024-10-27 23:37:03

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

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 属性在您的委托中,该委托并不存在。)

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

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 }

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

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.

锦上情书 2024-10-27 23:37:03

您也可以直接访问模型,例如

Text { text: myModel[myId.currentIndex].text }

You could alternatively access the model directly, with something like

Text { text: myModel[myId.currentIndex].text }
战皆罪 2024-10-27 23:37:03

您可以使用 get() 函数访问 ListModel 的 ListElement。

Text { text: myModel.get(myId.currentIndex).text }

You can access a ListElement of ListModel using get() function.

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