1

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.

1 Answers1

0

When a component is being loaded a parent is not set and it becomes set only after call of setParentItem method. It can be fixed by setting anchor when parent changes.

// comp.qml
Rectangle {
    onParentChanged: { anchors.top = parent.top; }
}
Kakadu
  • 2,837
  • 19
  • 29
  • Thanks, this worked for my simplified case, but not for real case where actual referring is done in the component very deep in the comp.qml hierarchy and which also may have different depths. – Hayk Shahparonyan Mar 24 '15 at 17:58
  • also the same thing could be done by calling beginCreate(), setParentItem() and finishCreate(), In my case I want to be able to refer them by id not by calling parent, which is actually a parameter of component itself. – Hayk Shahparonyan Mar 24 '15 at 18:00