use the function QmlObject::createObjectFromSource
This commit is contained in:
parent
31c442f39a
commit
222e132ecd
@ -50,7 +50,7 @@ public:
|
||||
delete root.data();
|
||||
}
|
||||
|
||||
void errorPrint();
|
||||
void errorPrint(QQmlComponent *component);
|
||||
void execute(const QUrl &source);
|
||||
void scheduleExecutionEnd();
|
||||
void minimumWidthChanged();
|
||||
@ -71,7 +71,7 @@ public:
|
||||
bool delay : 1;
|
||||
};
|
||||
|
||||
void QmlObjectPrivate::errorPrint()
|
||||
void QmlObjectPrivate::errorPrint(QQmlComponent *component)
|
||||
{
|
||||
QString errorStr = "Error loading QML file.\n";
|
||||
if(component->isError()){
|
||||
@ -181,7 +181,7 @@ void QmlObject::completeInitialization()
|
||||
return;
|
||||
}
|
||||
if (d->component->status() != QQmlComponent::Ready || d->component->isError()) {
|
||||
d->errorPrint();
|
||||
d->errorPrint(d->component);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -195,7 +195,7 @@ void QmlObject::completeInitialization()
|
||||
//d->root = d->component->create();
|
||||
|
||||
if (!d->root) {
|
||||
d->errorPrint();
|
||||
d->errorPrint(d->component);
|
||||
}
|
||||
|
||||
#ifndef NDEBUG
|
||||
@ -205,6 +205,29 @@ void QmlObject::completeInitialization()
|
||||
emit finished();
|
||||
}
|
||||
|
||||
QObject *QmlObject::createObjectFromSource(const QUrl &source)
|
||||
{
|
||||
QQmlComponent *component = new QQmlComponent(d->engine, this);
|
||||
component->loadUrl(source);
|
||||
component->create(d->incubator, d->engine->rootContext());
|
||||
while (!d->incubator.isReady() && d->incubator.status() != QQmlIncubator::Error) {
|
||||
QCoreApplication::processEvents(QEventLoop::AllEvents, 50);
|
||||
}
|
||||
QObject *object = d->incubator.object();
|
||||
|
||||
if (!component->isError() && d->root && object) {
|
||||
//memory management
|
||||
component->setParent(object);
|
||||
object->setProperty("parent", QVariant::fromValue(d->root.data()));
|
||||
return object;
|
||||
|
||||
} else {
|
||||
d->errorPrint(component);
|
||||
delete component;
|
||||
delete object;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#include "moc_qmlobject.cpp"
|
||||
|
@ -158,6 +158,14 @@ public:
|
||||
*/
|
||||
QQmlComponent *mainComponent() const;
|
||||
|
||||
/**
|
||||
* Creates and returns an object based on the provided url to a Qml file
|
||||
* with the same QQmlEngine and the same root context as the amin object,
|
||||
* that will be the parent of the newly created object
|
||||
* @param source url where the QML file is located
|
||||
*/
|
||||
QObject *createObjectFromSource(const QUrl &source);
|
||||
|
||||
public Q_SLOTS:
|
||||
/**
|
||||
* Finishes the process of initialization.
|
||||
|
@ -137,20 +137,15 @@ void AppletInterface::init()
|
||||
pkg.setPath("org.kde.toolbox");
|
||||
|
||||
if (pkg.isValid()) {
|
||||
QQmlComponent *toolBoxComponent = new QQmlComponent(m_qmlObject->engine(), this);
|
||||
toolBoxComponent->loadUrl(QUrl::fromLocalFile(pkg.filePath("mainscript")));
|
||||
QObject *toolBoxObject = toolBoxComponent->create(engine->rootContext());
|
||||
QObject *toolBoxObject = m_qmlObject->createObjectFromSource(QUrl::fromLocalFile(pkg.filePath("mainscript")));
|
||||
|
||||
QObject *containmentGraphicObject = m_qmlObject->rootObject();
|
||||
|
||||
if (containmentGraphicObject && toolBoxObject) {
|
||||
//memory management
|
||||
toolBoxComponent->setParent(toolBoxObject);
|
||||
toolBoxObject->setProperty("parent", QVariant::fromValue(containmentGraphicObject));
|
||||
|
||||
containmentGraphicObject->setProperty("toolBox", QVariant::fromValue(toolBoxObject));
|
||||
} else {
|
||||
delete toolBoxComponent;
|
||||
delete toolBoxObject;
|
||||
}
|
||||
} else {
|
||||
@ -515,9 +510,7 @@ void AppletInterface::geometryChanged(const QRectF &newGeometry, const QRectF &o
|
||||
return;
|
||||
}
|
||||
|
||||
QQmlComponent *component = new QQmlComponent(m_qmlObject->engine(), this);
|
||||
component->loadUrl(QUrl::fromLocalFile(applet()->containment()->corona()->package().filePath("ui", "CompactApplet.qml")));
|
||||
m_compactUiObject = component->create(m_qmlObject->engine()->rootContext());
|
||||
m_compactUiObject = m_qmlObject->createObjectFromSource(QUrl::fromLocalFile(applet()->containment()->corona()->package().filePath("ui", "CompactApplet.qml")));
|
||||
|
||||
QObject *compactRepresentation = 0;
|
||||
|
||||
@ -525,12 +518,13 @@ void AppletInterface::geometryChanged(const QRectF &newGeometry, const QRectF &o
|
||||
if (m_compactUiObject) {
|
||||
QQmlComponent *compactComponent = m_qmlObject->rootObject()->property("compactRepresentation").value<QQmlComponent *>();
|
||||
|
||||
if (!compactComponent) {
|
||||
compactComponent = new QQmlComponent(m_qmlObject->engine(), this);
|
||||
compactComponent->loadUrl(QUrl::fromLocalFile(applet()->containment()->corona()->package().filePath("ui", "DefaultCompactRepresentation.qml")));
|
||||
if (compactComponent) {
|
||||
compactRepresentation = compactComponent->create(m_qmlObject->engine()->rootContext());
|
||||
} else {
|
||||
compactRepresentation = m_qmlObject->createObjectFromSource(QUrl::fromLocalFile(applet()->containment()->corona()->package().filePath("ui", "DefaultCompactRepresentation.qml")));
|
||||
}
|
||||
compactRepresentation = compactComponent->create(m_qmlObject->engine()->rootContext());
|
||||
if (compactRepresentation) {
|
||||
|
||||
if (compactRepresentation && compactComponent) {
|
||||
compactComponent->setParent(compactRepresentation);
|
||||
} else {
|
||||
delete compactComponent;
|
||||
@ -538,9 +532,6 @@ void AppletInterface::geometryChanged(const QRectF &newGeometry, const QRectF &o
|
||||
}
|
||||
|
||||
if (m_compactUiObject && compactRepresentation) {
|
||||
//for memory management
|
||||
component->setParent(m_compactUiObject.data());
|
||||
|
||||
//put compactRepresentation in the icon place
|
||||
compactRepresentation->setProperty("parent", QVariant::fromValue(m_compactUiObject.data()));
|
||||
m_compactUiObject.data()->setProperty("compactRepresentation", QVariant::fromValue(compactRepresentation));
|
||||
@ -558,9 +549,7 @@ void AppletInterface::geometryChanged(const QRectF &newGeometry, const QRectF &o
|
||||
|
||||
//failed to create UI, don't do anything, return in expanded status
|
||||
} else {
|
||||
qWarning() << component->errors();
|
||||
m_expanded = true;
|
||||
delete component;
|
||||
}
|
||||
|
||||
emit expandedChanged();
|
||||
|
@ -135,9 +135,7 @@ void ContainmentInterface::appletAddedForward(Plasma::Applet *applet)
|
||||
|
||||
//if an appletGraphicObject is not set, we have to display some error message
|
||||
} else if (applet && contGraphicObject) {
|
||||
QQmlComponent *component = new QQmlComponent(qmlObject()->engine(), applet);
|
||||
component->loadUrl(QUrl::fromLocalFile(containment()->corona()->package().filePath("ui", "AppletError.qml")));
|
||||
QObject *errorUi = component->create();
|
||||
QObject *errorUi = qmlObject()->createObjectFromSource(QUrl::fromLocalFile(containment()->corona()->package().filePath("ui", "AppletError.qml")));
|
||||
|
||||
if (errorUi) {
|
||||
errorUi->setProperty("visible", false);
|
||||
|
Loading…
Reference in New Issue
Block a user