0

每次我在 qmlscene 中运行以下代码时都会发生此错误。在代码中,有 3 个嵌套ListView的 s 用 s 动态填充ListModel。内部ListView称为 daylistView ,外部称为 dayCalendar 和第三个月的dayCalendar。它们中的每一个都相应地代表一天中的小时,日和月MouseArea 。当我单击时,会打开一个窗口,因此我可以将 a 的文本设置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++
                                      }
                                    }
                                  }
                }
4

1 回答 1

0

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

于 2021-10-05T22:14:08.383 回答