void TestSimpleQmlLoad::loadSignal3() { QQmlEngine *engine = new QQmlEngine; const QString TEST_FILE(":/testqml/testsignal3.qml"); QQmlComponent* component = load(engine, TEST_FILE); QVERIFY(component); SignalTester st; engine->rootContext()->setContextProperty("st", &st); QObject *myObject = component->create(); QVERIFY(myObject != NULL); st.sendSig(); QVariant ret; QMetaObject::invokeMethod(myObject, "getSubWidth1", Q_RETURN_ARG(QVariant, ret)); QVERIFY(ret.toInt() == 10); st.sendSig(); QMetaObject::invokeMethod(myObject, "getSubWidth1", Q_RETURN_ARG(QVariant, ret)); QVERIFY(ret.toInt() == 20); delete component; delete engine; }
void TestSimpleQmlLoad::loadAlias2() { QQmlEngine *engine = new QQmlEngine; const QString TEST_FILE(":/testqml/testalias2.qml"); QQmlComponent* component = load(engine, TEST_FILE); QVERIFY(component); QObject *myObject = component->create(); QVERIFY(myObject != NULL); QVariant v = myObject->property("color"); QVERIFY(!v.isNull()); qWarning() << v.toString(); QVERIFY(v.toString() == "#333333"); QObject *r1 = myObject->findChild<QObject*>("r1"); r1->setProperty("color", "#999999"); v = myObject->property("color"); QVERIFY(v.toString() == "#999999"); QObject *r0 = myObject->findChild<QObject*>("r0"); QVERIFY(r0->property("color").toString() == "#999999"); delete component; delete engine; }
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; }
void TestSimpleQmlLoad::compileAndLoadSignal2() { const QString TEST_FILE(":/testqml/testsignal2.qml"); QQmlEngine *engine = new QQmlEngine; QQmlComponent* component = compileAndLoad(engine, TEST_FILE); QVERIFY(component); SignalTester st; engine->rootContext()->setContextProperty("st", &st); QObject *myObject = component->create(); QVERIFY(myObject != NULL); QVariant var = myObject->property("sigReceived"); QVERIFY(!var.isNull()); QVERIFY(var.toBool() == false); st.sendSig(); var = myObject->property("sigReceived"); QVERIFY(!var.isNull()); QVERIFY(var.toBool() == true); delete component; delete engine; }
void TestSimpleQmlLoad::loadSignal1() { QQmlEngine *engine = new QQmlEngine; const QString TEST_FILE(":/testqml/testsignal1.qml"); QQmlComponent* component = load(engine, TEST_FILE); QVERIFY(component); SignalTester st; QObject *myObject = component->create(); QVERIFY(myObject != NULL); QVariant var = myObject->property("complete1"); QVERIFY(!var.isNull()); QVERIFY(var.toInt() == 12); QVERIFY(st.val == -1); QMetaObject::invokeMethod(myObject, "sendSig"); QVERIFY(st.val == -1); QObject::connect(myObject, SIGNAL(sig1(int)), &st, SLOT(rcvMsg(int))); QMetaObject::invokeMethod(myObject, "sendSig"); QVERIFY(st.val == 1); delete component; delete engine; }
/** * 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; }
/** * 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(); } }
QObject_ *componentCreate(QQmlComponent_ *component, QQmlContext_ *context) { QQmlComponent *qcomponent = reinterpret_cast<QQmlComponent *>(component); QQmlContext *qcontext = reinterpret_cast<QQmlContext *>(context); if (!qcontext) { qcontext = qmlContext(qcomponent); } return qcomponent->create(qcontext); }
void TestSimpleQmlLoad::loadAlias3() { QVariant v; QQmlEngine *engine = new QQmlEngine; const QString TEST_FILE(":/testqml/testalias3.qml"); QQmlComponent* component = load(engine, TEST_FILE); QVERIFY(component); QObject *myObject = component->create(); QVERIFY(myObject != NULL); // test alias value in Component/delegate { QObject *i2 = myObject->findChild<QObject*>("i2"); QVERIFY(i2); v = i2->property("prop2"); QVERIFY(!v.isNull()); QVERIFY(v.toInt() == 222); QObject *i3 = myObject->findChild<QObject*>("i3"); QVERIFY(i3); v = i3->property("prop2"); QVERIFY(!v.isNull()); QVERIFY(v.toInt() == 222); i3->setProperty("prop2", 999); v = i2->property("prop2"); QVERIFY(!v.isNull()); QVERIFY(v.toInt() == 999); } // test alias value in non Component/delegate when Component/delegate exist // in the qml document { v = myObject->property("prop"); QVERIFY(!v.isNull()); QVERIFY(v.toInt() == 111); QObject *i4 = myObject->findChild<QObject*>("i4"); v = i4->property("prop"); QVERIFY(!v.isNull()); QVERIFY(v.toInt() == 111); myObject->setProperty("prop", 999); v = i4->property("prop"); QVERIFY(!v.isNull()); QVERIFY(v.toInt() == 999); } delete component; delete engine; }
void TestSimpleQmlLoad::compileAndLoadItem() { QQmlEngine *engine = new QQmlEngine; const QString TEST_FILE(":/testqml/testitem.qml"); QQmlComponent* component = compileAndLoad(engine, TEST_FILE); QVERIFY(component); QObject *myObject = component->create(); QQuickItem *item = qobject_cast<QQuickItem*>(myObject); int width = item->width(); // width = 100 QVERIFY(width == 100); delete component; delete engine; }
/* * This is simple test case to test loading a file with filename */ void TestCreateFile::testLoadSingleFile() { QQmlEngine *engine = new QQmlEngine; QmcLoader loader(engine); QQmlComponent *c = loader.loadComponent(tempDirPath(SUB_ITEM_QMC)); QVERIFY(c); QObject *obj = c->create(); QVariant var = obj->property("height"); QVERIFY(!var.isNull()); QVERIFY(var.toInt() == 20); delete obj; delete c; delete engine; }
void TestSimpleQmlLoad::loadSubItem2() { QQmlEngine *engine = new QQmlEngine; const QString TEST_FILE(":/testqml/testsubitem2.qml"); QQmlComponent* component = load(engine, TEST_FILE); QVERIFY(component); QObject *myObject = component->create(); QQuickItem *item = qobject_cast<QQuickItem*>(myObject); int height = item->height(); QVERIFY(height == 40); delete component; delete engine; }
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); }
void QPluginTypesTest::getTypesFromEngine(){ QString file = filePath("customtype.in.qml"); QByteArray code = readFile(file); if ( code == "" ) QFAIL("Unable to read input file."); QQmlEngine* engine = new QQmlEngine; QQmlComponent* component = new QQmlComponent(engine, QUrl::fromLocalFile(file)); if (component->isError() ) QFAIL("Failed to load component: customtype.in.qml"); component->create(); QList<const QQmlType*> typeList = QPluginTypesFacade::extractTypes("Stub", engine); QCOMPARE(typeList.length(), 1); QCOMPARE(QPluginTypesFacade::getTypeName(typeList[0]), QString("TypeStub")); }
QQuickWindow_ *componentCreateWindow(QQmlComponent_ *component, QQmlContext_ *context) { QQmlComponent *qcomponent = reinterpret_cast<QQmlComponent *>(component); QQmlContext *qcontext = reinterpret_cast<QQmlContext *>(context); if (!qcontext) { qcontext = qmlContext(qcomponent); } QObject *obj = qcomponent->create(qcontext); if (!objectIsWindow(obj)) { QQuickView *view = new QQuickView(qmlEngine(qcomponent), 0); view->setContent(qcomponent->url(), qcomponent, obj); view->setResizeMode(QQuickView::SizeRootObjectToView); obj = view; } return obj; }
void TestSimpleQmlLoad::compileAndLoadSubItem1() { QQmlEngine *engine = new QQmlEngine; const QString TEST_FILE(":/testqml/testsubitem1.qml"); const QString TEST_DEPENDENCY(":/testqml/SubItem.qml"); QList<QString> dependencies; dependencies.append(TEST_DEPENDENCY); QQmlComponent* component = compileAndLoad(engine, TEST_FILE, dependencies); QVERIFY(component); QObject *myObject = component->create(); QQuickItem *item = qobject_cast<QQuickItem*>(myObject); int width = item->width(); QVERIFY(width == 10); delete component; delete engine; }
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)); }
/* * This is test case that loads dependency automatically. Both success and fail case. */ void TestCreateFile::testLoadDependency() { QQmlEngine *engine = new QQmlEngine; QmcLoader loader(engine); loader.setLoadDependenciesAutomatically(false); QQmlComponent *c = NULL; c = loader.loadComponent(tempDirPath(SUB_ITEM_WITH_SCRIPT_QMC)); QVERIFY(!c); loader.setLoadDependenciesAutomatically(true); c = loader.loadComponent(tempDirPath(SUB_ITEM_WITH_SCRIPT_QMC)); QVERIFY(c); QObject *obj = c->create(); QVariant var = obj->property("height"); QVERIFY(!var.isNull()); QVERIFY(var.toInt() == 40); delete obj; delete c; delete engine; }
void TestSimpleQmlLoad::compileAndLoadSubItem2() { QQmlEngine *engine = new QQmlEngine; const QString TEST_FILE(":/testqml/testsubitem2.qml"); QList<QString> dependencies; dependencies.append(":/testqml/testscript1.js"); dependencies.append(":/testqml/testscript2.js"); dependencies.append(":/testqml/SubItemWithScript.qml"); QQmlComponent* component = compileAndLoad(engine, TEST_FILE, dependencies); QVERIFY(component); QObject *myObject = component->create(); QQuickItem *item = qobject_cast<QQuickItem*>(myObject); int height = item->height(); QVERIFY(height == 40); delete component; delete engine; }
void TestSimpleQmlLoad::loadComponent1() { QQmlEngine *engine = new QQmlEngine; const QString TEST_FILE(":/testqml/testcomponent1.qml"); QQmlComponent* component = load(engine, TEST_FILE); QVERIFY(component); QObject *myObject = component->create(); QVERIFY(myObject != NULL); QVariant var = myObject->property("c1"); QVERIFY(!var.isNull()); QVariant ret; QMetaObject::invokeMethod(myObject, "getSubWidth1", Q_RETURN_ARG(QVariant, ret)); QVERIFY(ret.toInt() == 10); delete component; delete engine; }
void TestSimpleQmlLoad::loadAlias1() { QQmlEngine *engine = new QQmlEngine; const QString TEST_FILE(":/testqml/testalias1.qml"); QQmlComponent* component = load(engine, TEST_FILE); QVERIFY(component); QObject *myObject = component->create(); QVERIFY(myObject != NULL); QVariant v = myObject->property("w"); QVERIFY(!v.isNull()); QVERIFY(v.toInt() == 100); myObject->setProperty("width", QVariant(200)); v = myObject->property("w"); QVERIFY(v.toInt() == 200); delete component; delete engine; }
void TestSimpleQmlLoad::compileAndLoadFunction1() { QQmlEngine *engine = new QQmlEngine; const QString TEST_FILE(":/testqml/testfunction1.qml"); QQmlComponent* component = compileAndLoad(engine, TEST_FILE); QVERIFY(component); QObject *myObject = component->create(); QVERIFY(myObject); QQuickItem *item = qobject_cast<QQuickItem*>(myObject); int width = item->width(); QVERIFY(width == 100); int height = item->height(); //QVERIFY(height == width * 2); item->setWidth(220); height = item->height(); QVERIFY(height == 20); delete component; delete engine; }
void TestSimpleQmlLoad::compileAndLoadListView1() { QQmlEngine *engine = new QQmlEngine; const QString TEST_FILE(":/testqml/testlistview1.qml"); QList<QObject*> l; l.append(new TestObject(1)); engine->rootContext()->setContextProperty("model1", QVariant::fromValue(l)); QQmlComponent* component = compileAndLoad(engine, TEST_FILE); QVERIFY(component); QObject *myObject = component->create(); QVERIFY(myObject != NULL); delete component; delete engine; }
void TestSimpleQmlLoad::loadScript1() { QQmlEngine *engine = new QQmlEngine; const QString TEST_FILE(":/testqml/testscript1.qml"); QQmlComponent* component = load(engine, TEST_FILE); QVERIFY(component); QObject *myObject = component->create(); QQuickItem *item = qobject_cast<QQuickItem*>(myObject); int width = item->width(); QVERIFY(width == 100); int height = item->height(); QVERIFY(height == 201); item->setWidth(15); height = item->height(); QVERIFY(height == 31); delete component; delete engine; }
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(); }
void TestSimpleQmlLoad::compileAndLoadBinding1() { QQmlEngine *engine = new QQmlEngine; const QString TEST_FILE(":/testqml/testbinding1.qml"); QQmlComponent* component = compileAndLoad(engine, TEST_FILE); QVERIFY(component); QObject* myObject = component->create(); QVERIFY(myObject); QQuickItem *item = qobject_cast<QQuickItem*>(myObject); int height = item->height(); QVERIFY(height == 62); QList<QQuickItem *> childs = item->childItems(); QVERIFY(childs.count() == 1); QQuickItem *child = childs[0]; QVERIFY(child->height() == 62); item->setHeight(2); QVERIFY(child->height() == 2); delete component; delete engine; }
void TestSimpleQmlLoad::compileAndLoadScript1() { QQmlEngine *engine = new QQmlEngine; const QString TEST_FILE(":/testqml/testscript1.qml"); const QString TEST_SCRIPT(":/testqml/testscript1.js"); QList<QString> dependencies; dependencies.append(TEST_SCRIPT); QQmlComponent* component = compileAndLoad(engine, TEST_FILE, dependencies); QVERIFY(component); QObject *myObject = component->create(); QQuickItem *item = qobject_cast<QQuickItem*>(myObject); int width = item->width(); QVERIFY(width == 100); int height = item->height(); QVERIFY(height == 201); item->setWidth(15); height = item->height(); QVERIFY(height == 31); delete component; delete engine; }
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; }