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
Developer
hungcotac
Posts: 79
Registered: ‎01-05-2013
My Device: BB10
My Carrier: Viettel
Accepted Solution

findChild() function doesnt work

[ Edited ]

I have some code:

 

qml

ListView {
                    id: expenseList
                    objectName: expenseList
                             
                    
                        
                         
                }

 and cpp:

QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

	// create root object for the UI
	AbstractPane *root = qml->createRootObject<AbstractPane>();
	// set created root object as a scene
	ListView *expense = root->findChild<ListView*>("expenseList");
	if (expense) {
		ArrayDataModel *model = new ArrayDataModel;
		model->append("Apple");
		model->append("Google");
		model->append("Microsoft");
		expense->setDataModel(model);
		qDebug() << "listview finded";
	} else {
		qDebug() << "listview cannot find";
	}

	app->setScene(root);

 Why this code always return "listview cannot find". I don't know where was my code wrong? any one help me

Please use plain text.
Developer
Zmey
Posts: 1,512
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: findChild() function doesnt work

Please show the complete QML.

If ListView is a root object then createRootObject already returns a pointer to it and findChild is not needed. Try:

ListView *expense = qml->createRootObject<ListView>();

Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Developer
hungcotac
Posts: 79
Registered: ‎01-05-2013
My Device: BB10
My Carrier: Viettel

Re: findChild() function doesnt work

import bb.cascades 1.0

// creates one page with a label
Page {
    Container {
        layout: DockLayout {}
        ListView {
            id: expenseList
            objectName: expenseList
        }
    }
}

 Here is my QML file

Please use plain text.
Developer
dishooom
Posts: 103
Registered: ‎12-31-2012
My Device: Blackberry 9810
My Carrier: Verizon

Re: findChild() function doesnt work

Object Name has to be in quotes...

 

import bb.cascades 1.0

// creates one page with a label
Page {
    Container {
        layout: DockLayout {}
        ListView {
            id: expenseList
            objectName: "expenseList"
        }
    }
}
Please use plain text.