/** * Initializes a component from a script */ void ScriptingService::initComponent(Script script) { const QString path = script.getScriptPath(); log("loading script file: " + path); const QUrl fileUrl = QUrl::fromLocalFile(path); ScriptComponent scriptComponent; QQmlComponent *component = new QQmlComponent(_engine); component->loadUrl(fileUrl); QObject *object = component->create(); if (component->isReady() && !component->isError()) { scriptComponent.component = component; scriptComponent.object = object; _scriptComponents[script.getId()] = scriptComponent; // call the init function if it exists if (methodExistsForObject(object, "init()")) { QMetaObject::invokeMethod(object, "init"); } // outputMethodsOfObject(object); if (methodExistsForObject(object, "onNoteStored(QVariant)")) { QObject::connect(this, SIGNAL(noteStored(QVariant)), object, SLOT(onNoteStored(QVariant))); } } else { qWarning() << "script errors: " << component->errors(); } }
/** * 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; }
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; }
int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); // регистриуем QML типы qmlRegisterType<ChatClient>("ChatClient", 1,0, "ChatClient"); qmlRegisterType<ChatUser>("ChatUser", 1,0, "ChatUser"); qmlRegisterType<Message>("Message", 1,0, "Message"); qRegisterMetaType<ChatUser::ChatUserId>("ChatUserId"); QQmlEngine *engine = new QQmlEngine(); // close window -> quit app app.connect(engine, SIGNAL(quit()), SLOT(quit())); ChatClient * client = new ChatClient; // регистрируем QML доступные переменные engine->rootContext()->setContextProperty("client", client); QQmlComponent *component = new QQmlComponent(engine); component->loadUrl(QUrl("qrc:/qml/main.qml")); QObject *object = component->create(); QQuickWindow *window = qobject_cast<QQuickWindow *>(object); window->show(); return app.exec(); }
int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); qmlRegisterType<Window>("People", 1, 0, "Window"); qmlRegisterType<Person>("People", 1, 0, "Person"); QQmlApplicationEngine engine; QQmlComponent *component = new QQmlComponent(&engine); QObject::connect(&engine, SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit())); component->loadUrl(QUrl(QStringLiteral("qrc:/main.qml"))); QObject *appWindow = component->create(); //these three lines are simply to show that we can change the name value //of this QObject *persona = appWindow->findChild<QObject*>("persona"); if (persona) persona->setProperty("name", "Blanco"); //these three lines are simply to show that we can change the text value //of this (comment or uncomment these lines to see that we can change any //property in the qml from c++ code /* QObject *label = appWindow->findChild<QObject*>("label"); if (label) label->setProperty("text", QString("Ramon (changed from c++ code)")); */ QQuickWindow *window = qobject_cast<QQuickWindow *>(appWindow); QSurfaceFormat surfaceFormat = window->requestedFormat(); window->setFormat(surfaceFormat); window->show(); return app.exec(); }