Using C++ model for MonthGrid in calendar
-
Hi everyone, I'm trying to implement a C++ based model for calendar. My Qt version is 6.6. According to Qt documentation calendar should look like this:
ColumnLayout { MonthGrid { month: root.month year: root.year delegate: DayDelegate {} } }
Nice and simple. Hovewer, I wanted to use a C++ model for MonthGrid. But it does not have any model property. To clarify - I have some data (basically it's more like a log) in tree model. I wanted to create a proxy to pass this data to calendar view and use in DayDelegate. It would be the most elegant way to provide data for calendar in my opinion. Is it even possible?
-
So if I understand it right, you want to show inside a MonthGrid the days for which you have data to log ?
-
So if I understand it right, you want to show inside a MonthGrid the days for which you have data to log ?
@ankou29666 Almost. I want to show all calendar, but I want to highlight days with data, e.g. add background color, add some icons maybe. I can certanly do it without model, but for this I will have to iterate through all data tree, as these elements are not sorted by date.
-
I would try something like
Item { id: root YourModel { id: dataModel // should provide a bool dataForDate(date) method } MonthGrid { id: monthGrid delegate: Text { required property var model horizontalAlignment: Text.AlignHCenter verticalAlignment: Text.AlignVCenter opacity: model.month === control.month ? 1 : 0 text: model.day font: control.font color: dataModel.hasDataForDate (model.date) ? "green" : "red" } } }
The model for the MonthGrid's delegate is the MonthGrid, not your data model.
Of course with this example your model would have to provide a function accepting a date as parameter returning a boolean whether or not there's information for that date. The delegate used must also have access to your model.I don't know whether you wrote your model in C++ or QML, but especially in the first case, keep in mind that in Qt/C++, date and time are completely distinct types (QDate and QTime, and QDateTime combining both). The dirty trick of javascript is there's only a Date object that combines date and time.
In other words, JS's Date is equivalent to Qt's QDateTime. This caused me a few issues.
So the three parameters (day, month, year) version might be an option to consider.