Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

MonthGrid wrong current index



  • Hi guys.
    I'm trying to create a datepicker with QuickControls2, but I have a weird error. When I click on a date the calendar highlight the previous one and returns the previous date. Is there something I missing?

    error.jpg

    MonthGrid{
                id: monthDaysGrid
                width: 304
                height: cellSize * 5
    
                anchors{
                    top: daysRow.bottom
                    horizontalCenter: parent.horizontalCenter
                }
    
                month: 0
                year: 2021
                locale: Qt.locale("en_US")
    
                delegate: Item{
                    id: dayDelegate
                    height: cellSize
                    width: cellSize
                    enabled: model.month === monthDaysGrid.month ? true : false
    
                    property bool highlighted: enabled && model.day === selectedDay && model.month === selectedMonth
    
                    Rectangle{
                        id: highlightIndicator
                        width: 36
                        height: 36
                        anchors.centerIn: dayDelegate
                        radius: 90
                        color: enabled && highlighted ? Theme.primaryColor : "transparent"
    
                        Label{
                            text: model.day
                            color: enabled ? (highlighted ? Theme.overPrimaryColor : Theme.overBackgroundColor) : Theme.disableColor
                            anchors.centerIn: parent
                        }
    
                        MouseArea {
                            anchors.fill: parent
                            onClicked: {
                                console.log("Current Day: " + selectedDay)
                                console.log("Selected Day: " + model.date.getDate())
                                console.log("Selected Year "+ model.date.getYear())
                                console.log("Clicked Item "+ index)
                                selectedDay = model.date.getDate()
                                selectedMonth = model.date.getMonth()
                                selectedYear = model.date.getFullYear()
                                selectedDate = selectedDay +", "+ selectedYear +" "+ months[selectedMonth]
                            }
                        }
    
                    }
                }
    
            }
    
    


  • It would be helpful to see the qml for MonthGrid.

    Meanwhile, I'm guessing that 'currentIndex' should be called when mouse is clicked.



  • @Markkyboy said in MonthGrid wrong current index:

    MonthGrid

    That's the weird part. I logged to index and it seems to be ok, but it keeps highlighting the previous item and choosing the previous date.



  • @Antonio-Ortiz
    I know nothing about QML(!) But how come you have selectedDay = model.date.getDate()? You want a day number, like you do for month & year, not a full date? I'm wondering whether your "1-off-from-click" is somehow connected to days counting from 1 versus 0 at some conversion point?



  • @JonB said in MonthGrid wrong current index:

    I follow this tutorial to implement the DatePicker.
    I think there is a miss count between the day count and the index, but I prefered to go this way



  • @Antonio-Ortiz
    There is nowhere in that code where date.getDate() is called, nor is a date assigned to a variable named like selectedDay. I suspect you need to recheck your logic.



  • Found the error. Don't call model.date.getYear(), model.date.getMonth() or model.date.getDate(), call instead model.year, model.month, and model.day


Log in to reply