Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Contributor
subramanyaraviteja
Posts: 20
Registered: ‎09-12-2012
My Device: Developer

Qml values to cpp

The User interface code is qml code and i want to write logic in CPP then how to get that controls values in cpp

Developer
raj_jyani
Posts: 107
Registered: ‎05-11-2011
My Device: BlackBerry Z10 LE

Re: Qml values to cpp

Hi,

 

You can do with set object name in QML and find reference using findchield in cpp.

(You can do same thing many way but it's jus example)

 

Qml

age {
    content: Container {
        objectName: "myContainer"
        Button {
            text: "Button"
        }
    }
}

 CPP

Container *container = root->findChild<Container*>("myContainer");

Also you can use  Q_INVOKABLE Macro 

Q_INVOKABLE
	QString getDevicePIN();

 for call cpp logic method from qml .

 

for more detail please check this link

https://developer.blackberry.com/cascades/documentation/ui/integrating_cpp_qml/index.html

 

Developer
nareshkodumuri
Posts: 125
Registered: ‎04-28-2012
My Device: BlackBerry 9800

Re: Qml values to cpp

Hi ,

 

       You can get the control reference from the qml file by using the findChild metod in cpp and you can set the values of a control by setter methods of that control.

 

For example:

main.qml

import bb.cascades 1.0
Page {
    Label {
        objectName: "objlabel"
        text: "some text here"
    }
}

 app.cpp

App::App()
{
    QmlDocument *qml = QmlDocument::create("main.qml");
    AbstractPane *root = qml->createRootNode<AbstractPane>();
    Label *lbl=root->findChild<Label *>("objlabel");
    lbl->setText("text changed from cpp");
    Application::setScene(root);
}

 This is a simple example for accessing reference of controls in qml file using cpp.

 

If u want to set the property of a control existed in qml do like this

 

Page{
Property alias text : lbl.text
Label{
    id:lbl
    text:"some text"
}
}

 app.cpp

root->setProperty("lbl","text changed");

 By these two ways u can access and change the control values from cpp.

 

For more information  just check the below link https://developer.blackberry.com/cascades/documentation/ui/integrating_cpp_qml/index.html

 

Contributor
subramanyaraviteja
Posts: 20
Registered: ‎09-12-2012
My Device: Developer

Re: Qml values to cpp

pls send me a simple example

Contributor
subramanyaraviteja
Posts: 20
Registered: ‎09-12-2012
My Device: Developer

Re: Qml values to cpp

if the screen has 15 controls than we have to call findchild 15 times no alternative for this na and the second  we can set the value in text field then how to get value present in textfield

Developer
nareshkodumuri
Posts: 125
Registered: ‎04-28-2012
My Device: BlackBerry 9800

Re: Qml values to cpp

Hi,

 

       You can get the reference of the control from qml file in cpp using findChild method and i don't know any other alternative is there or not.  Second ,you can get the value from the text field usingtext() method for that control.Please see the documentation of a control u can get the methods ,signals and slots available for that particular control.

 

Regards,

Naresh Kodumuri.

Developer
MFSI
Posts: 80
Registered: ‎09-03-2012
My Device: 9810

Re: Qml values to cpp

Ok don't do this.


    There is one alternative where you can have any number of controls in CPP. Just create them with in the App() or wherever you are setting the scene for the app.
    
    For root node of app's visual scene, you need to have a reference to an object of any subclass of AbstractPane(Page, TabbedPane, NavigationPane etc).


    You can have it like this.
 

 Page *root = new Page;

 
    Since a Page accepts a single control object as its content. So for several control objects you need to have a Container object that can have any no. of control objects within itself.

 

Container *container = new Container();
        container->setLayout(
                StackLayout::create().top(20.0f).direction(
                        LayoutDirection::TopToBottom));
                     

    Now create any no. of  Label controls you want with container as its parent.

 

Label *label1 = new Label(container);
Label *label2 = new Label(container);

    ...so on
    
    To root node(Page control) set container as its content.

 

 root->setContent(container);

 
    and then set this page as scene to the application UI

 

Application::setScene(page);

 
    This will avoid any interaction with qml files.

 

Always Success