8

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?

s4eed
  • 7,173
  • 9
  • 67
  • 104

1 Answers1

8

There shouldn't be any metatype registration necessary. All you need to is to call setContextProperty and pass the model by pointer:

QQmlContext* context = view->rootContext(); //view is the QDeclarativeView
context->setContextProperty( "_rosterItemModel", &mainClassInstance->m_rosterItemModel );

Use it in QML:

model: _rosterItemModel

By pointer is important, as QObject's are not copy-constructible and copying them would break their semantics anyway (as they have an "identity").

The alternative to registering the model directly is to register your main class' instance and using Q_INVOKABLE. In MainClass:

Q_INVOKABLE RosterItemModel* rosterItemModel() const;

Registering an instance of mainClass (mainClassInstance again is assumed to be a pointer):

context->setContextProperty( "_mainInstance", mainClassInstance );

In QML:

model: _mainInstance.rosterItemModel()
Frank Osterfeld
  • 24,815
  • 5
  • 58
  • 70
  • I registered MainClass to QML using qmlRegisterType in order to use MainClass signals and slots easily in QML by creating an instance of it like MainClass{id:mc} . Anyway . thank u :) – s4eed Aug 04 '12 at 07:05
  • 2
    It seems, from experience, that in the second case registering the model class in necassary too: `qmlRegisterType("CPPIntegrate", 1, 0, "MainClass");` Also, using `const` modifier seems to break the interoperation. The last quirky bit is to always refer to the class with its full namespace qualification within the header's Q_PROPERTY and Q_INVOKABLE declarations. Besides, if one attemts to return some base class (like QAbstractItemModel) pointer, the interoperability will fail again, so beware. – mlvljr Aug 05 '12 at 15:07
  • How to get mainClassInstance? – Brent81 Feb 23 '13 at 14:39
  • Expose property from C++ to QML is ok, but why "get a pointer to m_rosterItemModel in qml file using a Q_INVOKABLE function in MainClass" fail? – Brent81 Feb 23 '13 at 15:15
  • Since mainClassInstance is also a QObject why aren't you also passing it by pointer in your second example? – johnbakers May 01 '14 at 13:00
  • @OpenLearner: The snippets assume that `mainClassInstance` is a `MainClass*`, so it's passed by pointer. – Frank Osterfeld May 02 '14 at 05:57