【问题标题】:There are any possibilities to replace delegate in the view using QML?有没有可能使用 QML 替换视图中的委托?
【发布时间】:2011-04-06 18:38:48
【问题描述】:

例如

 ListView {
     id: listView
     anchors.fill: parent
     anchors.margins: 20
     model: myModel
     delegate: myDelegate
     highlightFollowsCurrentItem: true
     focus: true
     ListModel {
         id: myModel

         ListElement {
             name: "Apple"; cost: 2.45
             attributes: [
                 ListElement { description: "Core" },
                 ListElement { description: "Deciduous" }
             ]
         isOnMouse: false
     }
 }

我使用 myDelegate 作为任何模型的默认值,但如果单击其中一个项目,我想为该项目使用另一个委托,仅用于该项目。

有可能吗?

【问题讨论】:

标签: model-view-controller qt delegates model qml


【解决方案1】:

在您的主要项目中定义:

property boolean isItem:false

在您的委托中:

onClicked: isItem=index==myIndex myIndex 描述您的特殊项目

在 listView 集合中:

delegate: isItem? otherDel : myDelegate

【讨论】:

  • 这不会回答问题,因为它会更改所有项目的委托,而不仅仅是点击项目的委托。
【解决方案2】:

如果您使用 QML Loader 元素作为您的委托,那么您应该能够更改其 sourcesourceComponent,从而有效地将您的委托更改为单个项目。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 2019-08-30
    相关资源
    最近更新 更多