Nominate our 2022 Qt Champions!

model gets not assigned to custom class

  • Hi guys,

    I have a custom component which implements a Q_PROPERTY like this:

    class NChartPlotter : public QQuickFramebufferObject
        Q_PROPERTY(NChartAbstractDataModel* dataModel READ dataModel WRITE setDataModel)

    In main.cpp I create an instance of a NChartAbstractDataModel derived class and I set it as a contextProperty:

    NTestChartDataModel dm; //NTestChartDataModel is a subclass of NChartAbstractDataModel
    engine.rootContext()->setContextProperty("charDataModel", &dm);

    in QML I want to assign my model to a my view-component:

            ChartPlot {
                id: renderer
                anchors.fill: parent
                textureFollowsItemSize: true
                dataModel: charDataModel

    setDataModel gets never called.

    What am I doing wrong here?
    btw. this is my first qml-application.


  • ok, maybe I should have looked in the application-output earlier...
    -> "Unable to assign NTestChartDataModel to [unknown property type]"

    I do I let qml know about this class?

    ok, I found that I have to use qRegisterMetaType to register my classType but I don't understand why I have to implement a copy-constructor?!
    Anyway, even if I implement a copy constructor to make the usage of qRegisterMetaType possible, I still get the "unknown property type" error on runtime.

  • You need to use qmlRegisterType, not just qRegisterMetaType, for any type which you expose to QML in such a way that you expect property resolution (including method invocation) to occur on an instance of that type.

Log in to reply