Cannot access QML object in C ++ code

I am working on a QML application (blackberry 10) and have a QML file like this:

import bb.cascades 1.0 Page { content: Container { id: containerID Button { id: button1 text: "text" onClicked: { } } Label { id: label1 text: "text" } } } 

now I want to access label1 in my C ++ code, so I have the following code:

 #include "app.hpp" #include <bb/cascades/Application> #include <bb/cascades/QmlDocument> #include <bb/cascades/AbstractPane> using namespace bb::cascades; App::App() { QmlDocument *qml = QmlDocument::create("main.qml"); //-- setContextProperty expose C++ object in QML as an variable //-- uncomment next line to introduce 'this' object to QML name space as an 'app' variable //qml->setContextProperty("app", this); AbstractPane *root = qml->createRootNode<AbstractPane>(); QObject *labelTest = root->findChild<QObject*>("label1"); if (labelTest) labelTest->setProperty("text", "yes!!"); Application::setScene(root); } 

Now I launch the application, but the label text does not change.

what's wrong?

+4
source share
2 answers

I found the answer myself. QML code:

 import bb.cascades 1.0 //-- create one page with a label and text Page { property alias labelText: label.text content: Container { Label { id: label text: "Label" } Button { objectName: "button" text: "Button" } } } 

and C ++ code:

 #include "app.hpp" #include <bb/cascades/Application> #include <bb/cascades/QmlDocument> #include <bb/cascades/AbstractPane> using namespace bb::cascades; App::App() { QmlDocument *qml = QmlDocument::create("main.qml"); //-- setContextProperty expose C++ object in QML as an variable //-- uncomment next line to introduce 'this' object to QML name space as an 'app' variable //qml->setContextProperty("app", this); AbstractPane *root = qml->createRootNode<AbstractPane>(); root->setProperty("labelText", "yes"); QObject *newButton = root->findChild<QObject*>("button"); if (newButton) newButton->setProperty("text", "New button text"); Application::setScene(root); } 
+5
source

Alternative solution ...

QML:

 import bb.cascades 1.0 //-- create one page with a label and text Page { property alias labelText: label.text content: Container { Label { id: label text: "Label" objectName: "label1" } Button { objectName: "button" text: "Button" } } } 

C ++

 #include "app.hpp" #include <bb/cascades/Application> #include <bb/cascades/QmlDocument> #include <bb/cascades/AbstractPane> using namespace bb::cascades; App::App() { QmlDocument *qml = QmlDocument::create("main.qml"); //-- setContextProperty expose C++ object in QML as an variable //-- uncomment next line to introduce 'this' object to QML name space as an 'app' variable //qml->setContextProperty("app", this); AbstractPane *root = qml->createRootNode<AbstractPane>(); QObject *labelTest = root->findChild<QObject*>("label1"); if (labelTest) labelTest->setText(QString("yes!!")); Application::setScene(root); } 

But the solution you present is much better.

0
source

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


All Articles