[Interest] Register QML type and setting context property

Juan Jose Casafranca jjcasmar at gmail.com
Fri Dec 9 00:23:22 CET 2016


Hi,

I'm having some issues trying to declare a new type in qml.

I have a class which contains a shared pointer to a second class.

class Grid : public QObject
{
Q_OBJECT
...
}

Class FObject : public QObject
{
Q_OBJECT
public:std::shared_ptr<const Grid> grid() const;
protected:
   std::shared_ptr<Grid> m_grid;
}

and a third class which uses the grid

class GridGeometry : public Qt3DRender::QGeometry
{Q_OBJECTQ_PROPERTY(const Grid *grid READ grid WRITE setGrid NOTIFY gridChanged)
public:
    const Grid *grid() const;
    void setGrid(const Grid *grid);

signals:
    void gridChanged(const Grid *grid);
protected:const Grid *m_grid;
}

When the FObject is created, it creates a new Grid that will be unique in
that FObject and there is no way to change it. Both FObject and Grid
doesn't have a default constructor.

Im trying to expose this in QML so I can do

    Entity
    {
        id: gridId

        GridGeometry
        {
            grid: _grid
        }

        components:[
            ColorMapMaterial
            {
            }

        ]

        Component.onCompleted: console.log(_grid)
    }

Where

    qmlRegisterType<GridGeometry>("com.mslab.kaseo", 1, 0, "GridGeometry");
    qmlRegisterUncreatableType<Grid>("com.mslab.kaseo", 1, 0, "Grid",
"Grid is not instantiable");

    QGuiApplication app(argc, argv);
    std::array<float, 4> aabb{0,10,0,10};
    FracturableObject obj(aabb, 512);

    QQuickView view;
    view.resize(500, 500);

    view.setResizeMode(QQuickView::SizeRootObjectToView);
    const Grid *grid = obj.grid().get();
    view.rootContext()->setContextProperty("_grid", grid);
    //view.rootContext()->setContextProperty("_grid", obj.grid().get());
    view.setSource(QUrl("qrc:/main.qml"));
    view.show();

But when I try to assign the grid property in the GridGeometry class I get
the following error:
qrc:/Scene.qml:40:19: Unable to assign bool to [unknown property type]
qrc:/Scene.qml: 40

I'm new in QML so I don't know what I'm doing wrong. I have read the
documentation but I' not capable os solving this issue. Any ideas?

The main purpose if to generate a Qt3D scene and assign the data through
QML (Grid is a class which contains values at the points that will be
rendered using the GridGeometry, derived from Qt3DRender::QGeometry)


Thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20161208/5ee9e220/attachment.html>


More information about the Interest mailing list