Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Manually update a Calendar
Qt 6.12 LTS is out! See what's new in the release blog

Manually update a Calendar

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 1.3k Views 2 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.
  • W Offline
    W Offline
    WScape
    wrote on last edited by WScape
    #1

    Hi,

    I created a custom Calendar with a custom C++ model (in addition to the standard calendar model). When this custom model is updated, the calendar must refresh its content but it seems I missed something.

    The custom Calendar use a delegate to draw each cell and use the custom model to retrieve some useful data.

    Here's my CalendarView.qml code :

    Calendar {
        property DateNoteModel customModel
    
        style: CalendarStyle {
            dayDelegate: Rectangle {
                Label {
                    text: styleData.date.getDate() + " " + customModel.getNotesCount(styleData.date)
                    anchors.centerIn: parent
                    color: styleData.selected ? "red" : "blue"
                }
            }
        }
    }
    

    And here's my main.qml code :

    ApplicationWindow {
        width: 640
        height: 480
        visible: true
    
        DateNoteModel {
            id: customModel
    
            Component.onCompleted: {
                customModel.addNote("My note", "2015-07-21")
            }
    
            // Called after customModel.addNote()
            onNoteAdded: { 
                calendarView.update()
            }
        }
    
        CalendarView {
            id: calendarView
            model: customModel
    
            anchors.fill: parent
        }
    }
    

    The call to CalendarView.update() doesn't...update the calendar. I must use an ugly trick like

    calendarView.showNextMonth()
    calendarView.showPreviousMonth()
    

    Did i missed something ?

    Thank you very much

    p3c0P 1 Reply Last reply
    0
    • W WScape

      Hi,

      I created a custom Calendar with a custom C++ model (in addition to the standard calendar model). When this custom model is updated, the calendar must refresh its content but it seems I missed something.

      The custom Calendar use a delegate to draw each cell and use the custom model to retrieve some useful data.

      Here's my CalendarView.qml code :

      Calendar {
          property DateNoteModel customModel
      
          style: CalendarStyle {
              dayDelegate: Rectangle {
                  Label {
                      text: styleData.date.getDate() + " " + customModel.getNotesCount(styleData.date)
                      anchors.centerIn: parent
                      color: styleData.selected ? "red" : "blue"
                  }
              }
          }
      }
      

      And here's my main.qml code :

      ApplicationWindow {
          width: 640
          height: 480
          visible: true
      
          DateNoteModel {
              id: customModel
      
              Component.onCompleted: {
                  customModel.addNote("My note", "2015-07-21")
              }
      
              // Called after customModel.addNote()
              onNoteAdded: { 
                  calendarView.update()
              }
          }
      
          CalendarView {
              id: calendarView
              model: customModel
      
              anchors.fill: parent
          }
      }
      

      The call to CalendarView.update() doesn't...update the calendar. I must use an ugly trick like

      calendarView.showNextMonth()
      calendarView.showPreviousMonth()
      

      Did i missed something ?

      Thank you very much

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi @WScape, Not sure but can you try adding the note not in Component.onCompleted of model ? May be try adding it on a button click.

      157

      W 1 Reply Last reply
      0
      • p3c0P p3c0

        Hi @WScape, Not sure but can you try adding the note not in Component.onCompleted of model ? May be try adding it on a button click.

        W Offline
        W Offline
        WScape
        wrote on last edited by WScape
        #3

        Hi @p3c0 !

        Thanks for your reply. This code is just a test, I didn't make any "real" UI, that's why I used Component.onCompleted. But I'll try your solution and come back :)

        It seems the Calendar isn't updated because the delegate doesn't use any property (because of the property binding). If I declare a fake bool property in CalendarView and I use this trick :

        (beware, it's very very very ugly)

        // main.qml
        [...]
        onNoteAdded: {
            calendarView.fakeProp = !calendarView.fakeProp
        }
        
        // CalendarView.qml
        [...] // delegate
        Label {
            text: if (fakeProp || !fakeProp ) styleData.date.getDate() + " " + model.getNotesCount(styleData.date)
        }
        

        It works but...omg it's just...horrible.

        Unfortunately, the model.getNotesCount() function used above isn't a property but just a Q_INVOKABLE.
        ('model' subclasses QObject and acts like a wrapper around a QMap)

        I think I must create a property that makes sense in this context.

        Sorry for my bad english

        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
        • Unsolved