30 lines
994 B
C++
30 lines
994 B
C++
#ifndef DYNAMICQMLCOMPONENT_H
|
|
#define DYNAMICQMLCOMPONENT_H
|
|
|
|
#include <QGuiApplication>
|
|
#include <QQmlApplicationEngine>
|
|
#include <QQmlComponent>
|
|
#include <QQuickView>
|
|
#include <QDebug>
|
|
|
|
class DynamicQmlComponent : public QQmlComponent {
|
|
Q_OBJECT
|
|
public:
|
|
explicit DynamicQmlComponent(const QByteArray &qmlData, const QString &name, QQmlEngine *engine, QObject *parent = nullptr)
|
|
: QQmlComponent(engine, parent) {
|
|
setData(qmlData, QUrl("qrc:/")); // Устанавливаем данные
|
|
|
|
if (isReady()) {
|
|
qmlRegisterUncreatableType<DynamicQmlComponent>(name.toUtf8().constData(), 1, 0, name.toUtf8().constData(), "Not creatable in QML");
|
|
engine->setObjectOwnership(this, QQmlEngine::CppOwnership);
|
|
} else {
|
|
qCritical() << "Ошибка загрузки компонента:" << errors();
|
|
}
|
|
}
|
|
|
|
Q_INVOKABLE QObject* createInstance() {
|
|
return create();
|
|
}
|
|
};
|
|
#endif // DYNAMICQMLCOMPONENT_H
|