Nominate our 2022 Qt Champions!

[Solved] QAbstractListModel and QML considerations

  • Hi folks,

    I am currently trying to design a « CalendarView » in QML, with a C++-side QAbstractListModel.
    However, I'm facing an issue, and don't know how to fix it. Here is a screenshot of what I'm trying
    to achieve :


    As you can see, my view consist in a simple GridView, populated by a C++ CalendarModel, which
    is a subclass of QAbstractListModel, defining the following roles :

    @enum Role { DayNumberRole = Qt::UserRole + 1, MonthNumberRole, MonthNameRole, DateRole };@

    This is working fine, however, I don't know how to expose the « items » (the green rectangles) into
    my QML View. I tried returning a QList<CalendarItem *> on my data() function, bound to a given role
    (like ItemsRole), but didn't succeed to.

    What is the best way to do it ? I would like to keep the model-view driven architecture, keeping a
    C++-side model, populated by the user and then a QML view displaying all the datas.

  • I finally figured out by myself how to solve my problem.

    As I said, for the « ItemsRole » role on my data() function, I am now returning a simple QList<QObject *> (which is, actually, a QList of CalendarItem *), which I am using as a model QML-side.

Log in to reply