A
A
Alexander2017-03-20 08:29:47
Qt
Alexander, 2017-03-20 08:29:47

How to create a QML type?

It is necessary to create models of a group of physical bodies with the ability to edit their parameters without recompilation. I decided to use QML for this business - a convenient syntax, you can declare functions for a specific calculation of parameters. Everything is cool, but I would like to move away from Quick as much as possible.
Created 2 classes:

class Model : public QObject
{
    Q_OBJECT

public:
    explicit Model(QObject *parent = Q_NULLPTR);
}

class Anode : public QObject
{
    Q_OBJECT

public:
    explicit Anode(QObject *parent = Q_NULLPTR);
}

Registered types in main.cpp :
int main(int argc, char *argv[])
{
    qmlRegisterType<Model>("Model", 1, 0, "Model");
    qmlRegisterType<Anode>("Model", 1, 0, "Anode");
    // ...
}

I create a QML file:
import Model 1.0

Model {
    Anode {
    }
}

Download qml file:
QQmlEngine engine;
QQmlComponent component(&engine);
component.loadUrl(QUrl("types/type1.qml"));
QObject *myObject = component.create();

I get an error: QQmlComponent: Component is not ready
I load it differently:
QQmlApplicationEngine engine;
engine.load(QUrl("types/type1.qml"));

I get an error: types/type1.qml:4 Cannot assign to non-existent default property
Well, what is a default property whose name is not displayed in the debug?
It is worth removing the Anode object from the Model, how everything works.
Okay, I add Q_PROPERTY(Anode* anode READ anode WRITE setAnode NOTIFY anodeChanged) to the Model and edit the qml file:
import Model 1.0

Model {
    anode: Anode {
    }
}

I get an error: types/type1.qml:4 Unexpected object assignment
If both of these objects are inherited not from QObject , but from QQuickItem , then everything will work, but I don't need Item with all its x, y, width, height, etc. ., especially given that these parameters will be overridden by some model entities. How to defeat this Qml?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2017-03-20
@wxmaper

Ok, I figured this out:

Model {
    anode: Anode {
    }
}

It was necessary to additionally declare types through the QML_DECLARE_TYPE macro:
QML_DECLARE_TYPE(Model)
QML_DECLARE_TYPE(Anode)

But with this the error still persists...
Model {
    Anode {
    }
}

Looks like some magic macro is needed :)
Very tricky Qt! Climbed into QQuickItem and peeped what he has there ... And he has the Q_CLASSINFO macro involved.
In general, the problem was solved by adding a macro like this:
class Model : public QObject
{
    Q_OBJECT

    Q_PROPERTY(Anode* anode READ anode WRITE setAnode NOTIFY anodeChanged)

    Q_PROPERTY(QObject* item WRITE addItem)
    Q_CLASSINFO("DefaultProperty", "item")

public:
    explicit Model(QObject *parent = Q_NULLPTR);

public slots:
    void addItem(QObject* item);
}

When an Anode object is created, the Model::addItem method is called . And there you can already set the Anode object as the parent of the Model object.

J
Jacob E, 2017-03-20
@Zifix

It seems to me that you are trying to use QML in a non-standard way, so you have to immediately climb into the jungle. To begin with, I advise you to read this series of articles: https://habrahabr.ru/post/181712/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question