66 lines
1.7 KiB
C++
66 lines
1.7 KiB
C++
#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<QQuickItem*>(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);
|
|
}
|