[Interest] multiple QQuickWidgets with one QQmlEngine

shh at openmailbox.org shh at openmailbox.org
Mon Mar 23 17:32:07 CET 2015


Hi all.

I've got a problem and hope you can help me.

I have multiple QQmlWidgets where different instances of same components 
should be shown.
The components are created at runtime and use specific properties from 
it's parent widget.
The problem is, that there are multiple nested items in those 
components, so they are loading very slow.
I've decided to load them all to QQmlEngine at application preload 
stage, and after just create them when necessary.
So all my QQmlWidgets got same QQmlEngine and same root context.
To differ properties of different widgets I'm creating every instance of 
component in separate context (one per QQmlWidget).
After creating I'm setting parent item of newly created object to root 
item of QQmlWidget;
The problem is that the root item is referenced in the dynamically 
created objects and they could not find it.
Can you suggest any solution? I'm posting simplified version of my code 
below.


---main.cpp

QQmlEngine* e = new QQmlEngine;
QWidget* ww1 = new QWidget;
QWidget* ww2 = new QWidget;

QQuickWidget* w1 = new QQuickWidget(e, ww1);
QQuickWidget* w2 = new QQuickWidget(e, ww2);
w1->setSource(QUrl::fromLocalFile("main.qml"));
w2->setSource(QUrl::fromLocalFile("main.qml"));

QQmlComponent* comp = new QQmlComponent(e, "comp.qml");
QQmlContext* c1 = new QQmlContext(e);
QQmlContext* c2 = new QQmlContext(e);

QQuickItem* it1 = qobject_cast<QQuickItem*>(comp->create(c1));
it1->setParentItem(w1->rootObject());
QQuickItem* it2 = qobject_cast<QQuickItem*>(comp->create(c2));
it2->setParentItem(w2->rootObject());
ww1->show();
ww2->show();


--- main.qml

Rectangle {
   id: root
}

--- comp.qml

Rectangle {
   anchors.top : root.top
}


--- the error
comp.qml - ReferenceError: root is not defined


Thanks in advance.




More information about the Interest mailing list