Qml C++ Find Child

c++, qml, qt, signals-slots

Solution

`findChild` doesn't look for the `id` but the `objectName` property which you can simply add inside the `MainPage` object (see the documentation):

initialPage: MainPage {
    objectName: "MainPage"      
    tools: toolBarLayout
}

You could also access that object through the `initialPage` property of the `rootObject` without the need to add a `objectName` property:

QObject * mainPage = QDeclarativeProperty(rootObject, "initialPage").object();
if (mainPage)
     QDeclarativeProperty(mainPage, "toets").write(3);

Problem

I have a main.qml with a MainPage.qml inserted through: ``` initialPage: MainPage {tools: toolBarLayout} ``` because I choose to make it for Symbian. When i try: ``` QObject *mainPage = rootObject->findChild<QObject*>("MainPage"); if (mainPage) QDeclarativeProperty(mainPage, "toets").write(3); ``` the message doesn't come through but there are no errors, I have also tried connecting a SIGNAL to a SLOT on MainPage with the "if (mainPage)" but it also has no response. I have managed to get a signal through to main though but when I try: ``` function changeNum(num) { MainPage.changeNum(num) } ``` The function never gets executed because I don't get a message from console.log at all unlike I do when the function on main runs. I also know the other methods didn't work because the also didn't log a message or execute the rest of their function. I think the problem might lie in MainPage not being created as an element with a id. Do you know what might be causing this?

Original source