1
votes

After collecting various information on several posts on StackOverflow, I managed to dynamically create my QML component from some Qt C++ code.

Unfortunately, right after the creation, the QQmlComponent::setProperty method doesn't seem to have any effect. I can see the red rectangle but it doesn't go to x/y position that I want.

Anyone has any idea ?

Maybe there's something wrong on the "x: xPos" "y: yPos" property bindings ?

QML code:

import QtQuick 2.2

Rectangle {
  property int xPos: 0;
  property int yPos: 0;

  width: 100
  height: 100
  x: xPos
  y: yPos

  color: "red";
  radius: 10
}

C++ code (m_view is a QQuickView):

QQuickItem *twoDView = qobject_cast<QQuickItem*>(m_view->rootObject()->findChild<QObject *>("myView"));
QQmlComponent myComponent(m_view->engine(), QUrl("qrc:/myItem.qml"));
QQuickItem *newView = qobject_cast<QQuickItem*>(myComponent.create());
newView->setParentItem(twoDView);
myComponent.setProperty("xPos", x);
myComponent.setProperty("yPos", y);
1
Please don't add "(SOLVED)" to your title or include the solution in the body of the question. The way to indicate that the problem has been solved is to accept an answer (even if you posted it yourself).Keith Thompson

1 Answers

1
votes

OK, didn't try hard enough before posting. Sorry !

The solution is to call setProperty of newView and not myComponent !

newView->setProperty("xPos", x);
newView->setProperty("yPos", y);

Does the trick