Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QML declare new types
Qt 6.11 is out! See what's new in the release blog

QML declare new types

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 683 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jjcasmar
    wrote on last edited by
    #1

    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_OBJECT
    Q_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)

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved