typeerror: cannot read property 'newdaycalendar' of null in nested listview with listmodel dynamically created qml
typeerror: cannot read property 'newdaycalendar' of null in nested listview with listmodel dynamically created qml
每次我在 qmlscene 中 运行 下面的代码时都会发生此错误。在代码中有 3 个嵌套的 ListView
s,它们动态填充 ListModel
s。
里面的ListView
叫daylistView,外面的dayCalendar,第三个monthofdayCalendar。它们中的每一个都代表一天中的小时,日期和月份 accordingly.There 是一个 MouseArea
,当我点击时,会打开一个 window,这样我就可以设置一个 [=15] 的文本=]特定时间。
当我运行代码点击鼠标区域时,打开window设置标签文本,如果我更改显示的日期,上面的错误是displayed.But连续 2 或 3 次然后代码 运行ning 没有错误,直到我从另一个月中选择一天。按照同样的方法,在我手动更改日期 2 或 3 次后,错误不再显示。
我该如何解决这个问题,因为每次显示此错误时我都无法在 Label
notesLabel 中设置文本。
下面是经过一些推导的代码,因此更加紧凑:
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++
}
}
}
}
经过大量研究和时间,解决方案只是在 monthofdayCalendar ListView
中设置 currentIndex
,属性 monthName 而不是 calendarMonth.selectedDate.getMonth() .现在代码运行没有错误。
每次我在 qmlscene 中 运行 下面的代码时都会发生此错误。在代码中有 3 个嵌套的 ListView
s,它们动态填充 ListModel
s。
里面的ListView
叫daylistView,外面的dayCalendar,第三个monthofdayCalendar。它们中的每一个都代表一天中的小时,日期和月份 accordingly.There 是一个 MouseArea
,当我点击时,会打开一个 window,这样我就可以设置一个 [=15] 的文本=]特定时间。
当我运行代码点击鼠标区域时,打开window设置标签文本,如果我更改显示的日期,上面的错误是displayed.But连续 2 或 3 次然后代码 运行ning 没有错误,直到我从另一个月中选择一天。按照同样的方法,在我手动更改日期 2 或 3 次后,错误不再显示。
我该如何解决这个问题,因为每次显示此错误时我都无法在 Label
notesLabel 中设置文本。
下面是经过一些推导的代码,因此更加紧凑:
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++
}
}
}
}
经过大量研究和时间,解决方案只是在 monthofdayCalendar ListView
中设置 currentIndex
,属性 monthName 而不是 calendarMonth.selectedDate.getMonth() .现在代码运行没有错误。