类型错误:无法使用列表模型动态创建的 QML 在嵌套列表视图中读取 null 的属性'newdaycalendar'



每次我在qmlscene中运行以下代码时都会发生此错误。在代码中有3个嵌套的ListViews,这些ListModels动态填充。内部的ListView称为daylistView,外部的dayCalendar和第三个月的dayCalendar。每一个都代表一天中的小时、一天和一个月。有一个MouseArea,当我点击,打开一个窗口,所以我可以设置Label的文本为特定的小时。

当我运行代码并在鼠标区域单击时,打开设置标签文本的窗口,显示上述错误。但是如果我连续改变显示的日期2或3次,那么代码就会正常运行,直到我从另一个月选择一天。相同的方法错误停止后显示一天后我改变手动2或3次。

如何解决这个问题,因为每次显示这个错误时,我都不能在LabelnotesLabel中设置文本。

下面是带有一些演绎的代码,因此更紧凑:

ListView{
id: monthofdayCalendar
width: dayView.width*.58
height: dayView.height-100
x:10
y:20
interactive: false
highlightRangeMode: ListView.StrictlyEnforceRange
boundsBehavior:Flickable.StopAtBounds
orientation: Qt.Horizontal
clip: true
model:monthlistModel//12
currentIndex : calendarMonth.selectedDate.getMonth()
highlightMoveDuration : 200
highlightMoveVelocity : 1000
delegate: Item {
width:monthofdayCalendar.width
height:monthofdayCalendar.height
property int monthofdaycalendarIndex: index
property var newdayCalendar: dayCalendar
Label {
id: monthnameLabel
x:400
y:20
text:Qt.locale("en_US").standaloneMonthName((monthName===calendarMonth.selectedDate.getMonth() ? monthName : calendarMonth.selectedDate.getMonth()), Locale.LongFormat) + calendarMonth.selectedDate.toLocaleDateString(Qt.locale("en_US"), " yyyy")//calendarMonth.selectedDate.getFullYear(),index,calendarMonth.selectedDate.getDate() ).getMonth()index
font.pointSize: 18
}
ListView {
id:dayCalendar
x: 0
y: 50
width: monthofdayCalendar.width
height: monthofdayCalendar.height
interactive: false
highlightRangeMode: ListView.StrictlyEnforceRange
boundsBehavior:Flickable.StopAtBounds
orientation: Qt.Horizontal
clip: true
currentIndex : calendarMonth.selectedDate.getDate()
model:daylistModel//32
highlightMoveDuration : 500
highlightMoveVelocity : 1000
delegate : Item{
id:calDelegate
width: dayCalendar.width
height: dayCalendar.height
property int calIndex: index
property var dayList: daylistView
Row {
id: eventDateRow
width: parent.width
height: eventDayLabel.height
spacing: 10
Label {
id: eventDayLabel
text: day===0 ? calendarMonth.selectedDate.getDate() : day
font.pointSize: 35
}
Column {
height: eventDayLabel.height
Label {
text:Qt.locale("en_US").standaloneDayName((new Date(calendarMonth.selectedDate.getFullYear(),calendarMonth.selectedDate.getMonth(),(day===0 ? calendarMonth.selectedDate.getDate() : day))).getDay(), Locale.LongFormat) //Qt.locale("en_US").standaloneDayName(calendarMonth.selectedDate.getDay(), Locale.LongFormat)
font.pointSize: 14
}
/* Label {
text: Qt.locale("en_US").standaloneMonthName(calendarMonth.selectedDate.getMonth())
+ calendarMonth.selectedDate.toLocaleDateString(Qt.locale("en_US"), " yyyy")
font.pointSize: 12
}*/
}
}
ListView{
id:daylistView
width:dayCalendar.width
height:dayCalendar.height - 60
boundsBehavior:Flickable.StopAtBounds
highlightRangeMode: ListView.ApplyRange//StrictlyEnforceRange
orientation: Qt.Vertical
anchors.top:eventDateRow.bottom
clip:true
focus:true
model:hourlistModel
currentIndex : calendarMonth.selectedDate.getDay() === new Date().getDay()
&& calendarMonth.selectedDate.getDate() === new Date().getDate()
&& calendarMonth.selectedDate.getMonth() === new Date().getMonth() ? getHour() : 12
interactive: true
delegate: Item{
id:hourItem
property var hourTime: hourweeklistviewLabel
property var notetaking: notesLabel
width: daylistView.width
height: 60
MouseArea{
anchors.fill:parent
onClicked:{
windowLoader.active =true
daylistView.currentIndex=index
//dayCalendar.currentIndex = calDelegate.calIndex
//dayCalendar.currentIndex = Qt.binding(function() { return calendarMonth.selectedDate.getDate() })
}
}
Rectangle {
z:4
id:hourdaylistviewindexLine
y:getdayMinute()
width:hourItem.width
height:2
border.color: calendarMonth.selectedDate.getDate()===new Date().getDate()
&& getHour()+":00" === hourweeklistviewLabel.text
&& calendarMonth.selectedDate.getMonth() === new Date().getMonth()
?"red" : "transparent"
border.width:1
}
Rectangle {
z:4
id:hourline
anchors.verticalCenter: daylistView
width:daylistView.width
height:2
border.color: getHour()+":00" === hourweeklistviewLabel.text
&& calendarMonth.selectedDate.getDate()===new Date().getDate()
&& calendarMonth.selectedDate.getMonth() === new Date().getMonth()
&& getdayMinute()=== "00" ? "red": "lightgray"
border.width:1
}
Label{
z:4
id:hourweeklistviewLabel
anchors.verticalCenter: parent
text: hour + ":00"
color: getHour()+":00" === hourweeklistviewLabel.text
&& calendarMonth.selectedDate.getDate() === new Date().getDate()
&& calendarMonth.selectedDate.getDay() === new Date().getDay()
&& calendarMonth.selectedDate.getMonth() === new Date().getMonth()
? systemPalette.highlight : "lightgray"
font.pointSize: 10
}
Label{
z:4
id:notesLabel
anchors.left:hourweeklistviewLabel.right
anchors.leftMargin: 30
text:""
}
}
}
ListModel{
id:hourlistModel
Component.onCompleted:{
for (var i = 0; i <=24; i++){
append(createListElement())
}
}
property int h:0
function createListElement(){
return {
hour : h++
}
}
}
}
}
ListModel{
id:daylistModel
Component.onCompleted:{
for (var j=0; j <= 31; j++){
append(createListElement())
}
}
property int dD:0
function createListElement(){
return {
day : dD++
}
}
}
}
}
ListModel{
id:monthlistModel
Component.onCompleted:{
for (var k=0; k <=11; k++){
append(createListElement())
}
}
property int mN:0
function createListElement(){
return {
monthName : mN++
}
}
}
}

经过大量的研究和时间,解决方案只是将monthofdayCalendarListView中的属性monthName设置为currentIndex,而不是calendarMonth.selectedDate.getMonth()。现在代码运行没有错误。

相关内容

  • 没有找到相关文章

最新更新