/** * Checks if the script can be used in a component */ bool ScriptingService::validateScript(Script script, QString &errorMessage) { const QString path = script.getScriptPath(); QFile file(path); if (!file.exists()) { errorMessage = tr("file doesn't exist"); return false; } const QUrl fileUrl = QUrl::fromLocalFile(path); QQmlEngine *engine = new QQmlEngine(); QQmlComponent *component = new QQmlComponent(engine); component->loadUrl(fileUrl); // we need the object to get all errors QObject *object = component->create(); bool result = component->isReady() && !component->isError(); if (!result) { errorMessage = component->errorString(); } delete(object); delete(component); return result; }
QObject *QFAppDispatcher::singletonObject(QQmlEngine *engine, QString package, int versionMajor, int versionMinor, QString typeName) { QString pattern = "import QtQuick 2.0\nimport %1 %2.%3;QtObject { property var object : %4 }"; QString qml = pattern.arg(package).arg(versionMajor).arg(versionMinor).arg(typeName); QObject* holder = 0; QQmlComponent comp (engine); comp.setData(qml.toUtf8(),QUrl()); holder = comp.create(); if (!holder) { qWarning() << QString("QuickFlux: Failed to gain singleton object: %1").arg(typeName); qWarning() << QString("Error: ") << comp.errorString(); return 0; } QObject*object = holder->property("object").value<QObject*>(); holder->deleteLater(); if (!object) { qWarning() << QString("QuickFlux: Failed to gain singleton object: %1").arg(typeName); qWarning() << QString("Error: Unknown"); } return object; }
int main(int argc, char ** argv) { QApplication app(argc, argv); qmlRegisterType<Hider>("com.ics.demo", 1, 0, "Hider"); int rc = 0; QQmlEngine engine; QQmlComponent *component = new QQmlComponent(&engine); QObject::connect(&engine, SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit())); component->loadUrl(QUrl("../../Source/main.qml")); Hider::image_provider = new ImageProvider; engine.addImageProvider("images", Hider::image_provider); if (!component->isReady() ) { qWarning("%s", qPrintable(component->errorString())); return -1; } QObject *topLevel = component->create(); QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel); QSurfaceFormat surfaceFormat = window->requestedFormat(); window->setFormat(surfaceFormat); window->show(); rc = app.exec(); delete component; return rc; }
char *componentErrorString(QQmlComponent_ *component) { QQmlComponent *qcomponent = reinterpret_cast<QQmlComponent *>(component); if (qcomponent->isReady()) { return NULL; } if (qcomponent->isError()) { QByteArray ba = qcomponent->errorString().toUtf8(); return local_strdup(ba.constData()); } return local_strdup("component is not ready (why!?)"); }
void tst_qqmlenginecleanup::test_qmlClearTypeRegistrations() { //Test for preventing memory leaks is in tests/manual/qmltypememory QQmlEngine* engine; QQmlComponent* component; QUrl testFile = testFileUrl("types.qml"); qmlRegisterType<QObject>("Test", 2, 0, "TestTypeCpp"); engine = new QQmlEngine; component = new QQmlComponent(engine, testFile); QVERIFY(component->isReady()); delete engine; delete component; qmlClearTypeRegistrations(); //2nd run verifies that types can reload after a qmlClearTypeRegistrations qmlRegisterType<QObject>("Test", 2, 0, "TestTypeCpp"); engine = new QQmlEngine; component = new QQmlComponent(engine, testFile); QVERIFY(component->isReady()); delete engine; delete component; qmlClearTypeRegistrations(); //3nd run verifies that TestTypeCpp is no longer registered engine = new QQmlEngine; component = new QQmlComponent(engine, testFile); QVERIFY(component->isError()); QCOMPARE(component->errorString(), testFile.toString() +":46 module \"Test\" is not installed\n"); delete engine; delete component; }