Important: Please read the 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?


                id: monthDaysGrid
                width: 304
                height: cellSize * 5
                    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 && === selectedDay && model.month === selectedMonth
                        id: highlightIndicator
                        width: 36
                        height: 36
                        anchors.centerIn: dayDelegate
                        radius: 90
                        color: enabled && highlighted ? Theme.primaryColor : "transparent"
                            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: " +
                                console.log("Selected Year "+
                                console.log("Clicked Item "+ index)
                                selectedDay =
                                selectedMonth =
                                selectedYear =
                                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:


    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 = 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, or, call instead model.year, model.month, and

Log in to reply