首页 > 解决方案 > ListView B 内的 ListView A 未在使用加载程序 qml 加载的窗口中定义

问题描述

我有一个垂直ListView嵌套在下面的水平Listview中(它是通用代码,因为真正的代码太长了,我想知道我想要做什么):

ApplicationWindow{
       id:appwindow
       ............
       Item{
        id:dayView
        ...........
        ListView{
            id:dayCalendar 
            orientation: Qt.Horizontal
            model:31
            delegate: Item{
            ...............
            ListView{
                id:daylistView
                orientation: Qt.Vertical
                model:24
                delegate:Item{
                     id:hourItem
                     property string hourTime:hourweeklistviewLabel
                     property string notetaking:notesLabe 
                     .............
                     MouseArea{
                          anchors.fill:parent
                          onClicked:{
                          windowLoader.active =true
                          daylistView.currentIndex=index

                          }
                     }  
                     Rectangle{}
                     Label{
                       id:hourweeklistviewLabel
                     }
                     Label{
                       id:notesLabel                                        
                       anchors.left:hourweeklistviewLabel.right
                       anchors.leftMargin: 30
                       text:""
                     }//Label
                    }//delegate:Item
                   }//ListView
                  } //delegate:Item
                 }//ListView
                }//Item

MouseArea当我在垂直 ListView 内部单击时,还有一个加载程序会加载一个窗口:

Loader {
    id:windowLoader
    focus: true
    active:false
    sourceComponent: Window{
        id:inputWin
        title:"Enter Note"
        width:500
        height:300
        visible:true

        onClosing:{
            windowLoader.active=false
            daylistView.currentIndex = calendarMonth.selectedDate.getDate() === new Date().getDate()
                    && calendarMonth.selectedDate.getMonth() === new Date().getMonth()?getHour():12
        }
        TextField {
            id:title
            x:50
            y:20
            placeholderText :'Enter Note'
            text:daylistView.currentItem.notetaking.text
        }
        TextField{
            id:timeDate

            anchors.horizontalCenter: title.horizontalCenter
            anchors.top:title.bottom
            anchors.topMargin:10
            placeholderText :  calendarMonth.selectedDate.getDate() +"-"
                   + (calendarMonth.selectedDate.getMonth()+1)+"-"
                   + calendarMonth.selectedDate.getFullYear() + " "
                   + daylistView.currentItem.hourTime.text +":00"
                }

            Button {
                 id: button
                 text: qsTr("Add Note")
                 anchors.centerIn:parent

                 onClicked: {
                       if (title.text !==""){daylistView.currentItem.notetaking.text= title.text}
                       else{}

                    }
                }
            }
        }

我面临的问题是,ListView daylistView当我运行应用程序时未在其中定义,Window inputWin因此我无法使用窗口中的代码(和之间的双向绑定title.textdaylistView.currentItem.notetaking.text破坏并且daylistView.currentIndex为空)。我试图公开daylistView为属性,但 listview 仍然没有被定义。如何定义这个列表视图?

谢谢你。

标签: qtlistviewqmlloader

解决方案


这是有道理的,因为您正在创建 的多个实例daylistView,因此未定义您想要哪个实例。

ListView但是,您可以在根委托中公开as 属性并通过 使用它ListView.currentItem,前提是您currentIndexdayCalendar ListView

ListView{
    id: dayCalendar

    delegate: Item {
        id: calDelegate
        property int calIndex: index
        property var dayList: daylistView
        
        ListView {
            id: daylistView

            delegate: Item {
                MouseArea {
                    onClicked: {
                        dayCalendar.currentIndex = calDelegate.calIndex
                        daylistView.currentIndex = index
                        ...
                    }
                }
            }
        } 
    }

在加载的 qml 中:

TextField {
    id:title
    text: dayCalendar.currentItem.dayList.currentItem.notetaking.text
}

推荐阅读