I have a QML object, which can create the same objects inside of it. The function addChildRect is called from C++. Each object is provided with its unique id and objectName (for each object they are the same). I want to get access to them from C++ using QObject::findChild, but for dynamically created object this function always returns null pointer. My suggestion is, that this function parses only objects, that were in QML initially. How can I get access to dynamically created objects from C++?
Rect.qml
Rectangle {
color: "red"
function addChildRect(id,x,y,width,height)
{
var component;
component = Qt.createComponent("Rect.qml");
component.createObject(this, {
id:id,
objectName:id,
x:x,
y:y,
width:width,
height:height});
}
}
C++ code:
//find element
auto parentRectView = engine.rootObjects().first()->findChild<QObject*>(QString::number(id()));
//create element
QMetaObject::invokeMethod(parentRectView,"addChildRect",
Q_ARG(QVariant,id()),
Q_ARG(QVariant,m_position.x()),
Q_ARG(QVariant,m_position.y()),
Q_ARG(QVariant,m_size.height()),
Q_ARG(QVariant,m_size.width()));
QObject::findChild. Also, to avoid unnecessary actions you just can return pointer to created object fromaddChildRectto C++parent: thisin QML object construction code? Also could you please provide me simple example, how to return pointer from QML?Repeateron the QML side and providing the data from C++ via a customQAbstractListModelderived class.