Referencing id from string
-
wrote on 22 Jan 2024, 18:30 last edited by
In my QML I have a grid of 64 of a custom component with id of a1 to h8. The custom component contains functions that I can call such as c2.clearPiece(). My C++ is returning the c2 to me as a string that I store into a var called from, however when I try to run the function from.clearPiece() I get "TypeError: Property 'clearPiece' of object c2 is not a function". Is there any way that I can use the data in the string to reference to the id? I have also set up objectName in the custom component. Is there any way that I can use a getChild() type function on the grid to get hold of the object?
-
In my QML I have a grid of 64 of a custom component with id of a1 to h8. The custom component contains functions that I can call such as c2.clearPiece(). My C++ is returning the c2 to me as a string that I store into a var called from, however when I try to run the function from.clearPiece() I get "TypeError: Property 'clearPiece' of object c2 is not a function". Is there any way that I can use the data in the string to reference to the id? I have also set up objectName in the custom component. Is there any way that I can use a getChild() type function on the grid to get hold of the object?
wrote on 22 Jan 2024, 18:34 last edited by@Paul-Webster I guess you can find a child with objectName, but not with id.
int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; // Assuming you have a QML root item QQuickItem* rootItem = qobject_cast<QQuickItem*>(engine.rootObjects().first()); if (rootItem) { // Find child item by objectName QQuickItem* myObject = rootItem->findChild<QQuickItem*>("myObject"); if (myObject) { // Do something with myObject qDebug() << "Found child object with objectName 'myObject'"; } else { qDebug() << "Child object with objectName 'myObject' not found"; } } return app.exec(); }
-
@Paul-Webster I guess you can find a child with objectName, but not with id.
int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; // Assuming you have a QML root item QQuickItem* rootItem = qobject_cast<QQuickItem*>(engine.rootObjects().first()); if (rootItem) { // Find child item by objectName QQuickItem* myObject = rootItem->findChild<QQuickItem*>("myObject"); if (myObject) { // Do something with myObject qDebug() << "Found child object with objectName 'myObject'"; } else { qDebug() << "Child object with objectName 'myObject' not found"; } } return app.exec(); }
wrote on 22 Jan 2024, 18:43 last edited by@JoeCFD So I would need to access it through C++, I can't do it from within the QML?
-
@JoeCFD So I would need to access it through C++, I can't do it from within the QML?
wrote on 22 Jan 2024, 19:16 last edited by@Paul-Webster I think that I have it:
for(var i = 0; i < grid2.children.length; ++i)
if(grid2.children[i].objectName === from){
grid2.children[i].clearPiece()
} -
1/4