Passing QAbstractListModel to QML

  • Hi,
    I have created a c++ class derived from QAbstractListModel and registered (using root context) same with QML to display a data on the ListView. I am able to successfully do that. Usually we do register on main.cpp to let QML know about the class.
    The problem i have is on the model in QML, i need to filter the data on model by its type. So when i start filtering i need to clear the model and fetch data from database and loads back to model again. This is time consuming and at the same time my UI freezes until data is loaded. So i understud as my class is kept on main.cpp it resides on main thread and operation on that class will behave so.

    My implementation is as follows:
    on main.cpp:

    EventStatusModel *eventModel = EventStatusModel::instance();
    viewer.rootContext()->setContextProperty("eventModelObject", eventModel);

    on QML:
    model: eventModelObject

    So here on the model when i start filtering , i have created a worker thread which will access EventStatusModel and clear its data and fetches data from tables and loads back to EventStatusModel.

    Hope i have explained situation well. Here i need to know whether i can use any approaches to avoid freeze on UI

    Bala B

  • I have done an example similar to yours with sorting and filtering property.

    My example is described here in this blog

    And the complete source code is available here Git:/QMLSortFilterList

    Hope this helps!

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.