我有一個垂直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.text和之間的雙向系結daylistView.currentItem.notetaking.text被破壞并且daylistView.currentIndex為空)。我試圖公開daylistView為屬性,但串列視圖仍然沒有被定義。如何定義這個串列視圖?
謝謝你。
uj5u.com熱心網友回復:
這是有道理的,因為您正在創建 的多個實體daylistView,因此未定義您想要哪個實體。
但是,您可以ListView在根委托中公開as 屬性并通過 使用它ListView.currentItem,因為您currentIndex在dayCalendar 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
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/317326.html
