/** * 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(); } }
int main(int argc, char *argv[]) { int ret; QGuiApplication app(argc, argv); QQuickView *pParentView = new QQuickView(); pParentView->setResizeMode(QQuickView::SizeRootObjectToView); QQmlEngine *engine = pParentView->engine(); qmlRegisterType<TestListModel>("TestListModel",1,0,"TestListModel"); #if 1 QDir::setCurrent(QGuiApplication::applicationDirPath()); QmcLoader loader(engine); QQmlComponent *component = loader.loadComponent("../qml/main.qmc"); #else QQmlComponent *component = new QQmlComponent(engine, QUrl("../qml/main.qmc")); #endif if (!component) { qDebug() << "Could not load component"; return -1; } if (!component->isReady()) { qDebug() << "Component is not ready"; if (component->isError()) { foreach (const QQmlError &error, component->errors()) { qDebug() << error.toString(); } }
//![0] int main(int argc, char ** argv) { QGuiApplication app(argc, argv); QList<QObject*> dataList; dataList.append(new DataObject("Item 1", "red")); dataList.append(new DataObject("Item 2", "green")); dataList.append(new DataObject("Item 3", "blue")); dataList.append(new DataObject("Item 4", "yellow")); QQuickView view; view.setResizeMode(QQuickView::SizeRootObjectToView); QQmlContext *ctxt = view.rootContext(); //![0] #if 1 // add precompiled files QQmlEngine *engine = view.engine(); QQmlEnginePrivate::get(engine)->v4engine()->iselFactory.reset(new QV4::JIT::ISelFactory); QmcLoader loader(engine); QQmlComponent *component = loader.loadComponent(":/view.qmc"); if (!component) { qDebug() << "Could not load component"; return -1; } if (!component->isReady()) { qDebug() << "Component is not ready"; if (component->isError()) { foreach (const QQmlError &error, component->errors()) { qDebug() << error.toString(); } }
int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); if (!QResource::registerResource("qmc_res3.rcc") || !QResource::registerResource("qmc_res5.rcc")) { qDebug() << "Could not register resources."; return 1; } QQuickView view; view.setResizeMode(QQuickView::SizeRootObjectToView); QQmlEngine *engine = view.engine(); QmcLoader loader(engine); QQmlComponent *component = loader.loadComponent("qrc:/app.qmc"); if (!component) { qDebug() << "Could not load component"; return -1; } if (!component->isReady()) { qDebug() << "Component is not ready"; if (component->isError()) { foreach (const QQmlError &error, component->errors()) { qDebug() << error.toString(); } }
DiscoverTestExecutor(QObject* rootObject, QQmlEngine* engine, const QUrl &url) : QObject(engine) { connect(engine, &QQmlEngine::quit, this, &DiscoverTestExecutor::finish, Qt::QueuedConnection); QQmlComponent* component = new QQmlComponent(engine, url, engine); m_testObject = component->create(engine->rootContext()); if (!m_testObject) { qWarning() << "error loading test" << url << m_testObject << component->errors(); Q_ASSERT(false); } m_testObject->setProperty("appRoot", QVariant::fromValue<QObject*>(rootObject)); connect(engine, &QQmlEngine::warnings, this, &DiscoverTestExecutor::processWarnings); }
QQmlComponent* TestSimpleQmlLoad::load(QQmlEngine *engine, const QString &file) { QQmlEnginePrivate::get(engine)->v4engine()->iselFactory.reset(new QV4::JIT::ISelFactory); QUrl url("qrc" + file); QQmlComponent* component = new QQmlComponent(engine, url, QQmlComponent::PreferSynchronous); if (!component) return NULL; if (component->isError() || !component->isReady()) { printErrors(component->errors()); delete component; return NULL; } return component; }
void VCSlider::render(QQuickView *view, QQuickItem *parent) { if (view == nullptr || parent == nullptr) return; QQmlComponent *component = new QQmlComponent(view->engine(), QUrl("qrc:/VCSliderItem.qml")); if (component->isError()) { qDebug() << component->errors(); return; } m_item = qobject_cast<QQuickItem*>(component->create()); m_item->setParentItem(parent); m_item->setProperty("sliderObj", QVariant::fromValue(this)); }
void VCButton::render(QQuickView *view, QQuickItem *parent) { if (view == NULL || parent == NULL) return; QQmlComponent *component = new QQmlComponent(view->engine(), QUrl("qrc:/VCButtonItem.qml")); if (component->isError()) { qDebug() << component->errors(); return; } QQuickItem *item = qobject_cast<QQuickItem*>(component->create()); item->setParentItem(parent); item->setProperty("buttonObj", QVariant::fromValue(this)); }
QObject *ObjectNodeInstance::createComponentWrap(const QString &nodeSource, const QByteArray &importCode, QQmlContext *context) { QmlPrivateGate::ComponentCompleteDisabler disableComponentComplete; Q_UNUSED(disableComponentComplete) QQmlComponent *component = new QQmlComponent(context->engine()); QByteArray data(nodeSource.toUtf8()); data.prepend(importCode); component->setData(data, context->baseUrl().resolved(QUrl("createComponent.qml"))); QObject *object = component; QmlPrivateGate::tweakObjects(object); QQmlEngine::setContextForObject(object, context); QQmlEngine::setObjectOwnership(object, QQmlEngine::CppOwnership); if (component->isError()) { qWarning() << "Error in:" << Q_FUNC_INFO << component->url().toString(); foreach (const QQmlError &error, component->errors()) qWarning() << error; qWarning() << "file data:\n" << data; }
void QuickChatController::loadTheme(const QString &name) { QQuickItem *item = m_item; QString path = ThemeManager::path(QStringLiteral("qmlchat"), name); QString main = path % QLatin1Literal("/main.qml"); QQmlComponent component (DeclarativeView::globalEngine(), QUrl::fromLocalFile(main)); QObject *obj = component.create(); if (!obj) { qDebug() << component.errors(); return; } m_item = qobject_cast<QQuickItem*>(obj); QQmlEngine::setObjectOwnership(m_item, QQmlEngine::CppOwnership); QQmlProperty controllerProperty(m_item, QStringLiteral("controller")); controllerProperty.write(QVariant::fromValue(this)); emit itemChanged(m_item); delete item; }
QByteArray QQmlDataTest::msgComponentError(const QQmlComponent &c, const QQmlEngine *engine /* = 0 */) { QString result; const QList<QQmlError> errors = c.errors(); QTextStream str(&result); str << "Component '" << c.url().toString() << "' has " << errors.size() << " errors: '"; for (int i = 0; i < errors.size(); ++i) { if (i) str << ", '"; str << errors.at(i).toString() << '\''; } if (!engine) if (QQmlContext *context = c.creationContext()) engine = context->engine(); if (engine) { str << " Import paths: (" << engine->importPathList().join(QStringLiteral(", ")) << ") Plugin paths: (" << engine->pluginPathList().join(QStringLiteral(", ")) << ')'; } return result.toLocal8Bit(); }