Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Manually update a Calendar

    QML and Qt Quick
    2
    3
    710
    Loading More Posts
    • 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
      WScape last edited by 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

      p3c0 1 Reply Last reply Reply Quote 0
      • p3c0
        p3c0 Moderators @WScape last edited by

        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 Reply Quote 0
        • W
          WScape @p3c0 last edited by WScape

          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 Reply Quote 0
          • First post
            Last post