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
defoo
Posts: 11
Registered: ‎02-22-2011
My Device: Not Specified

How to add custom-QML-Control with C++?

Hi,

 

I have created a custom control with QML and now I am trying to add it to my app with C++. In my main QML UI, I have a button which will call the C++ function "App::injectContainer" when pressed.

 

However, when I test the app, it will complain that it cannot find the qml file where the custom control is defined in.

(asset:///SettingInputBox.qml: Network error)

I also try just specifying "SettingInputBox.qml" (without the "asset:///" in front). This time, the app would complain that it cannot find the file.

 

 

This is the definition of the C++ function.

 

void App::injectContainer() {

	component = new QDeclarativeComponent(engine, QUrl("asset:///SettingInputBox.qml"));

	if (component->isLoading())
		QObject::connect(component,
				SIGNAL(statusChanged(QDeclarativeComponent::Status)), this,
				SLOT(continueLoading()));
	else
		continueLoading();

}

void App::continueLoading() {
	if (component->isError()) {
		qWarning() << component->errors();
	} else {
		Control *myObject = qobject_cast<bb::cascades::Control*>(
				component->create());
		mRootContainer->add(myObject);
	}
}

 

 Any help would be much appreciated!

Please use plain text.
BlackBerry Development Advisor (Retired)
amarcon
Posts: 155
Registered: ‎07-16-2012
My Device: Dev Alpha
My Carrier: Bell

Re: How to add custom-QML-Control with C++?

Hi defoo,

 

I've made some tests here based on your code, you can try something like this instead:

 

void App::injectContainer() {

 QmlDocument *qml = QmlDocument::create().load("SettingInputBox.qml");

 if (!qml->hasErrors()) {
    Container *control= qml->createRootNode<Container>();
    if (control) {
    	myContainer->add(control);
    }
 }
}

 

Hope it helps.

 

Cheers,

Please use plain text.