#include "dynamicloader.h" DynamicLoader::DynamicLoader(QQmlEngine* engine, QObject* parent) : QObject(parent), m_engine(engine) { } void DynamicLoader::createComponent(const QString& qmlFile, QQuickItem* parentItem, QQuickItem **createdComponent) { QQmlComponent component(m_engine, QUrl(qmlFile)); if (component.isError()) { qDebug()<<"Error create component:" << component.errorString(); emit errorOccurred(component.errorString()); return; } QObject* object = component.create(); if (!object) { emit errorOccurred("Failed to create object"); return; } QQuickItem* item = qobject_cast(object); if (!item) { emit errorOccurred("Created object is not a QQuickItem"); object->deleteLater(); return; } item->setParent(parentItem); item->setParentItem(parentItem); item->setWidth(parentItem->width()); item->setHeight(parentItem->height()); if (createdComponent) { *createdComponent = item; } emit componentCreated(item); } void DynamicLoader::createComponentObject(const QString& qmlFile, QObject **createdComponent) { QQmlComponent component(m_engine, QUrl(qmlFile)); if (component.isError()) { qDebug()<<"Error create component:" << component.errorString(); emit errorOccurred(component.errorString()); return; } QObject* object = component.create(); if (!object) { emit errorOccurred("Failed to create object"); return; } // object->setParent(parentItem); if (createdComponent) { *createdComponent = object; } // emit componentCreated(object); }