Qml C ++ Find a child

I have main.qml with MainPage.qml inserted through:

initialPage: MainPage {tools: toolBarLayout} 

because I decided to do it for Symbian. When I try:

 QObject *mainPage = rootObject->findChild<QObject*>("MainPage"); if (mainPage) QDeclarativeProperty(mainPage, "toets").write(3); 

the message does not go through, but there are no errors, I also tried to connect the SIGNAL to the SLOT on the MainPage with "if (mainPage)", but it also did not respond. I managed to get the signal to the main one, but when I try:

 function changeNum(num) { MainPage.changeNum(num) } 

The function never starts, because I do not receive a message from console.log at all, unlike what I do when the main function starts. I also know that other methods did not work because they also did not register the message or perform other functions.

I think the problem may be that MainPage is not created as an element with an identifier. Do you know what could be causing this?

+6
source share
1 answer

findChild does not look for the id property, but the objectName property, which you can simply add inside the MainPage object (see the documentation ):

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

You can also access this object through the initialPage rootObject property without having to add the objectName property:

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

Source: https://habr.com/ru/post/916171/


All Articles