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)); }
//![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(); ctxt->setContextProperty("myModel", QVariant::fromValue(dataList)); #ifdef linux CPUTimer timer(CLOCK_PROCESS_CPUTIME_ID); #endif //![0] #if 1 // add precompiled files QQmlEngine *engine = view.engine(); QQmlEnginePrivate::get(engine)->v4engine()->iselFactory.reset(new QV4::JIT::ISelFactory); QmcLoader loader(engine); #ifdef linux timer.start(); #endif 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(); } }
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 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; }