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
Highlighted
Contributor
Posts: 26
Registered: ‎08-13-2013
My Device: Developer
My Carrier: Developer
Accepted Solution

Problem in setproperty

[ Edited ]

Hi,

I have a test.qml file and i am trying to change the text of a button and label from cpp file using setProperty, I found the component using findChild but still setproperty is not working 

Please help.

 

 

test.qml


import bb.cascades 1.0

Page {
    
    Container {
        objectName: "testContainer"
        Button {
            objectName: "myButton"
            text: "myButton"
            onClicked: {
            	app.test1();
                
      			}
        }
        
    }
}

 

 

applicationui.cpp


void ApplicationUI::test1()
{
	qDebug()<<"test1 called";

	QmlDocument *qml = QmlDocument::create("asset:///test.qml");
	//Container *c = qml->createRootObject<Container>();
	AbstractPane *root = qml->createRootObject<AbstractPane>();

	QObject *newContainer = root->findChild<QObject*>("testContainer");
	QObject *newButton = newContainer->findChild<QObject*>("myButton");
	
qDebug()<<newButton->property("text");//Printing "myButton"
	
	if(newButton)
	{
		newButton->setProperty("text", "New button text");
		qDebug()<<"Button Found";
	}
	else
	{
		qDebug()<<"Button not found";
	}

	qDebug()<<newButton->setProperty("text","my Button Text");
qDebug()<<newButton->property("text");//printing "my Button Text" }

 

"Button Found" is getting printed in console,

newButton->setProperty("text","my Button Text") is returning true.

Changes are happening, but are not visible in simulator

Developer
Posts: 145
Registered: ‎12-23-2012
My Device: BB10 Dev Alpha
My Carrier: wifi

Re: Problem in setproperty

Are you able to retrieve property using following API ?

 

QVariant QObject::property(const char * name) const

 

 

Contributor
Posts: 26
Registered: ‎08-13-2013
My Device: Developer
My Carrier: Developer

Re: Problem in setproperty

Yes, I can.
Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Problem in setproperty

Hi,

 

Most likely you're creating a second instance of QMLDocument and root pane and modifying that second instance. The original one remains unmodified. To reference the original one:

 

void ApplicationUI::test1()
{
	qDebug()<<"test1 called";

	AbstractPane *root = Application::instance()->scene();

	QObject *newContainer = root->findChild<QObject*>("testContainer");
	QObject *newButton = newContainer->findChild<QObject*>("myButton");
...

 Also, you can cast the result to appropriate classes directly:

Container *newContainer = root->findChild<Container *>("testContainer");
Button *newButton = newContainer->findChild<Button *>("myButton");

 findChild is recursive, so it's possible to search for button without retrieving the container first:

 

Button *newButton = root->findChild<Button *>("myButton");

setProperty is rarely needed. In most cases it's better to use the methods of corresponding class, such as Button's setText:

newButton->setText("text");

 

 


Andrey Fidrya, @zmeyc on twitter