【问题标题】:qt quick call index of listview from nested listview来自嵌套列表视图的列表视图的 qt 快速调用索引
【发布时间】:2012-11-14 21:27:58
【问题描述】:

我在 qml 中有一个带有嵌套列表视图的列表视图,如何在嵌套列表视图委托中获取父列表视图索引的索引?

代码示例:

ListView {
        id: listView
        anchors.fill: parent
        delegate: delegate
        model: myModel
    }

    Component {
        id: delegate
        Item {
            id: recipe
            width: listView.width
            height: 120


            Column {

                   // some text fields
                    ListView {
                        id: listView1
                        width: listView.width
                        height: 50
                        delegate: nextLevelDelegate
                        model: nextLevelList
                    }

                }
        }
    }

    Component {
        id: nextLevelDelegate
        Item{
                                        width: listView1.width
                                        height: 20
                                        Rectangle {
                                            id: background2
                                            x: 2; y: 2; width: parent.width - x*2; height: parent.height - y*2
                                            color: "lightgray"
                                            border.color: "gray"
                                            radius: 5
                                        }
                                        Text {
                                            anchors.fill: parent
                                            id:nextLevelButton
                                            horizontalAlignment:Text.AlignHCenter
                                            text: modelData
                                        }
                                        MouseArea
                                        {
                                          anchors.fill: parent
                                          id: mouseArea
                                          onClicked: {
                                             window.buttonPressed(nextLevelButton.text,listView.currentIndex);//I need parent index here for calling c++ method with it
                                          }
                                        }
                                    }

    }

【问题讨论】:

    标签: qt listview qml qt-quick


    【解决方案1】:

    将父列表视图的索引分配给不同的属性,这样它就不会被遮蔽。

    ListView {
       id: listView1
       property int parentIndex: index
       width: listView.width
       height: 50
       delegate: nextLevelDelegate
       model: nextLevelList
    

    }

    parentIndex 将在nextLevelDelegate 中作为listView1.parentIndex 提供。

    【讨论】:

      【解决方案2】:

      这对我有用:

      import QtQuick 2.15
      import QtQuick.Window 2.15
      
      Window {
      width: 640
      height: 480
      visible: true
      title: qsTr("Hello World")
      
      ListModel {
          id: listModelOne
          ListElement {vertical: 8}
          ListElement {vertical: 9}
      }
      ListModel {
          id: listModelTwo
          ListElement {horiz: 0}
          ListElement {horiz: 1}
      }
      ListView{
          id:verticalListView
          anchors.fill:parent
          model:listModelOne
      
          delegate: Item{
              id:vert_del
              width: 200
              height: 100
              ListView{
                  property int parentIndex: index
                  id:horizontalListView
                  width:verticalListView.width
                  height:100
                  orientation: ListView.Horizontal
                  model:listModelTwo
                  delegate: Item{
                      width:50
                      height:50
                  Text{
                      text:horiz
                      MouseArea{
                      anchors.fill:parent
                          onClicked: console.debug(horizontalListView.parentIndex)
                      }
                  }
                  }
              }
          }
      }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-20
        相关资源
        最近更新 更多