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

Manually update a Calendar



  • 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


  • Moderators

    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.



  • 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


Log in to reply