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: 84
Registered: ‎01-05-2013
My Device: BB10
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

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
Developer
hungcotac
Posts: 84
Registered: ‎01-05-2013
My Device: BB10

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

Developer
dishooom
Posts: 103
Registered: ‎12-31-2012
My Device: Blackberry 9810

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"
        }
    }
}