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
skyhawk92
Posts: 79
Registered: ‎02-04-2013
My Device: Z10 LE

Container setting background

Hi guys,

 

anyone have any idea why setting a background to container like this, would prevent application to start(application closes before it fully starts) ?

 

Page *appPage = root->findChild<Page*>("mainPage");
    	Container *container1 = appPage->findChild<Container*>("firstContainer");
    	container1->setBackground(Color::Green);

 

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

Re: Container setting background

The setBackground() expects an argument of ImagePaint or ColorPaint type and not a Color.

 

ColorPaint class is used for retrieving the colors used in  fields in BB' s native theme. Such as the textColor used in SystemDefaults. Specific RGB values cannot be used here at the moment.

 

http://developer.blackberry.com/cascades/reference/bb__cascades__colorpaint.html

 

Dishooom

Developer
oliver_kranz
Posts: 216
Registered: ‎09-18-2009
My Device: Z10

Re: Container setting background

In QML this works.

 

Container {
    background: Color.create("#FF00FF00")
}

 

Developer
skyhawk92
Posts: 79
Registered: ‎02-04-2013
My Device: Z10 LE

Re: Container setting background

Yes, in QML but in C++ ?

 

The point was to be able to change different "themes"(colors) of application through menu.

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

Re: Container setting background

One way to get this done is by adding a javascript function in your qml file to set the background property of the container as shown

 function setColorYellow() {
            bellContainer.background = Color.Yellow
        }

 Then, from the CPP file you can invoke this method for the container as

  Container* con = bb::cascades::Application::instance()->findChild<Container*>("bellContainer");
    QMetaObject::invokeMethod(con, "setColorYellow");

May be there's an easier way to get this done, but this is one that does work for me.

 

 - Dishooom

Developer
Zmey
Posts: 1,512
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: Container setting background

[ Edited ]

This should also work:

container1->setBackground(Color::fromARGB(0xff996633));

http://developer.blackberry.com/cascades/reference/bb__cascades__color.html

 

Or using a builder:

 

Container *someContainer = Container::create().background(Color::fromARGB(0xff996633));

There are also predefined values for colors like Color::Red.

 

The code in first post looks correct:

Container *container1 = appPage->findChild<Container*>("firstContainer");
    	container1->setBackground(Color::Green);

It could crash because Container with id 'firstContainer' wasn't found among the children of appPage.

You can check that container1 is not NULL by logging it's value or setting a breakpoint on next line and running the app in debugger. Also ensure that appPage itself is not NULL.

 


Andrey Fidrya, @zmeyc on twitter
Developer
skyhawk92
Posts: 79
Registered: ‎02-04-2013
My Device: Z10 LE

Re: Container setting background

Yep, appPage is NULL but I don't have any idea why ?

In QML it has objectName: mainPage
then in C++ I call Page *appPage = root->findChild<Page*>("mainPage");
Developer
skyhawk92
Posts: 79
Registered: ‎02-04-2013
My Device: Z10 LE

Re: Container setting background

If i findChild a button it works normaly( is not NULL),
but if try to find Page or Container they are NULL.
New Developer
smrg17
Posts: 5
Registered: ‎08-16-2014
My Device: Simulator

Re: Container setting background

You need to have an objectName attribute in your container. The findChild does not find by id: it looks for objectName: "the_name". That explains why it's NULL. This took me a long long time to figure out