Is it possible to setContextProperty local to a qml file displayed in a QQuickWidget??
-
I have an app with QMdiArea as the central widget and each QMdiSubWindow has a QQuickWidget which displays a qml item. I have another QObject which gets initialized in each QQuickWidget whoose signals I want to capture in the Qml as shown. I was able to do that by creating a separate instance of QQmlEngine which creates a new QQmlContext my question is that is that the right way? Is it possible to set a context property to a local file?
// SubWindow.cpp ... // class SubWindow : public QMdiSubWindow SubWindow::SubWindow(QObject* parent, QQmlEngine* engine) : QMdiSubWindow(parent) { // creating a new engine as I need separate 'interface' property for each subwindow QQmlEngine* newContentEngine = new QQmlEngine(this); if (newContentEngine->rootContext() && engine && engine->rootContext()) { // copy 'colors' from the parent engine newContentEngine->rootContext()->setContextProperty( "colors", engine->rootContext()->contextProperty("colors")); Content* cont = new Content(this, newContentEngine); setWidget(cont); } } ...
// ContentInterface.h ... class ContentInterface : public QObject { Q_OBJECT public: ContentInterface(QObject* parent); public signals: void dataChanged(); } ...
// Content.cpp ... // class Content : public QQuickWidget Content::Content(SubWindow* parent, QQmlEngine* contentEngine) : QQuickWidget(contentEngine, parent) { ContentInterface* interface_ = new ContentInterface(this); // the rootContext is local to the new Engine created so works! rootContext()->setContextProperty("interface", interface_); setResizeMode(QQuickWidget::SizeRootObjectToView); setSource(QUrl("qrc:/Content.qml")); } ...
// Content.qml Rectangle { Connections { target: interface onDataChanged : { ... } }
-
@Gbhutra said in Is it possible to setContextProperty local to a qml file displayed in a QQuickWidget??:
Is it possible to set a context property to a local file?
try QQmlComponent::create() with a newly created QQmlContext instance on which you have set the context property
QQmlComponent comp(engine); comp.loadUrl(QUrl("qrc:/MyComp.qml")); QQmlContext* context = new QQmlContext(engine); context->setContextProperty(...); QObject* obj = comp.create(context); context->setParent(obj); // Note: not 100% sure if thats correct, but otherwise context would leak the memory
-
Hi @Gbhutra
Can you have look at Integrating QML Code with Existing Qt UI Code
Good luck.
-
@Gbhutra said in Is it possible to setContextProperty local to a qml file displayed in a QQuickWidget??:
How can I embed a QQmlComponenet into a widget/QMdiSubWindow?
QQuickWidget* w = new QQuickWIdget( engine ); // use the same shared engine instance w->setResizeMode( QQuickWidget::SizeRootObjectToView ); w->setSource("qrc:/MyContainer.qml"); // where the MyContainer comp is a simple plain Item {} QQuickItem* root = w->rootObject(); QQuickItem* item = qobject_cast<QQuickItem*>(obj); // obj comes from the code of my previous post (the object created from the QQmlComponent) item->setParentItem( root ); QObject::connect(root, &QQuickItem::widthChanged, item, &QQuickItem::setWidth ); QObject::connect(root, &QQuickItem::heightChanged, item, &QQuickItem::setHeight );
@Pradeep-P-N said in Is it possible to setContextProperty local to a qml file displayed in a QQuickWidget??:
Can you have look at Integrating QML Code with Existing Qt UI Code
this link is related to QML1/Qt4, so not applicable