qt - 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.text
被daylistView.currentItem.notetaking.text
破坏并且daylistView.currentIndex
为空)。我试图公开daylistView
为属性,但 listview 仍然没有被定义。如何定义这个列表视图?
谢谢你。
解决方案
这是有道理的,因为您正在创建 的多个实例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
}
推荐阅读
- wordpress - 在产品画廊阵容中 - 使简短的摘录成为最小长度
- apache-spark - Exception while starting Spark History Server
- google-chrome - 如何在 cypress 中为 chrome 浏览器设置代理
- javascript - 如何在 stackblitz 示例中的每次单击时创建新的 ReplaySubjects?
- .net-core - 从 (3.1.101 x64 ) 升级/迁移 dotnet core Blazor 解决方案
- flutter - 如何在 Flutter 中创建视差页面过渡?
- ag-grid - AG Grid自定义过滤器React组件在升级到版本22后第一次打开时不调用afterGuiAttached
- javascript - 渲染错误:“TypeError:无法读取未定义的属性‘split’”
- grails - grails 4 - 使用代码进行本地化绑定时,BindingFormat 注释不绑定日期
- python - 我不明白“ValueError:没有足够的值来解包”