How to expose a C++ Model to QML

Viewed 6827

I'm writing a QML+Qt application . I defined a class like this :

class MainClass : public QObject
{
    Q_OBJECT

public:
    rosterItemModel m_rosterItemModel;
.
.
.
}

rosterItemModel model is a class derived from QAbstractListModel. I exposed MainClass to qml part using this function :

qmlRegisterType<MainClass>("CPPIntegrate", 1, 0, "MainClass");

Now I want to assign this model(m_rosterItemModel) from MainClass to model property of a ListView in QML. I tried the following ways but none of them were helpful :(

  • I tried to declare m_rosterItemModel as a PROPERTY using Q_PROPERTY . I couldn't do that because it said that QAbstractListModel is not copy-able.
  • I tried to get a pointer to m_rosterItemModel in qml file using a Q_INVOKABLE function in MainClass. But it wasn't helpful too.

Could someone help me?

1 Answers
Related