Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. MonthGrid wrong current index
Forum Updated to NodeBB v4.3 + New Features

MonthGrid wrong current index

Scheduled Pinned Locked Moved Solved QML and Qt Quick
9 Posts 4 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Antonio Ortiz
    wrote on last edited by
    #1

    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]
                            }
                        }
    
                    }
                }
    
            }
    
    
    1 Reply Last reply
    0
    • MarkkyboyM Offline
      MarkkyboyM Offline
      Markkyboy
      wrote on last edited by
      #2

      It would be helpful to see the qml for MonthGrid.

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

      Don't just sit there standing around, pick up a shovel and sweep up!

      I live by the sea, not in it.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Antonio Ortiz
        wrote on last edited by
        #3

        @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.

        JonBJ 1 Reply Last reply
        0
        • A Antonio Ortiz

          @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.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @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?

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Antonio Ortiz
            wrote on last edited by
            #5

            @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

            JonBJ 1 Reply Last reply
            0
            • A Antonio Ortiz

              @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

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @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.

              1 Reply Last reply
              1
              • A Offline
                A Offline
                Antonio Ortiz
                wrote on last edited by
                #7

                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

                H 1 Reply Last reply
                0
                • A Antonio Ortiz

                  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

                  H Offline
                  H Offline
                  Hit Tiger Tonight
                  wrote on last edited by
                  #8

                  @Antonio-Ortiz Hi, do you know how to update the current day of MonthGrid when system date changed?

                  H 1 Reply Last reply
                  0
                  • H Hit Tiger Tonight referenced this topic on
                  • H Hit Tiger Tonight

                    @Antonio-Ortiz Hi, do you know how to update the current day of MonthGrid when system date changed?

                    H Offline
                    H Offline
                    Hit Tiger Tonight
                    wrote on last edited by
                    #9

                    @Hit-Tiger-Tonight Workaround is like this:
                    https://forum.qt.io/topic/144297/how-to-update-the-current-day-of-monthgrid/3?_=1681442215899

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved