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. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Cascades Development

Reply
Contributor
soulgen
Posts: 26
Registered: ‎02-11-2013
My Carrier: TELE2

Color from C++ to QML

Hi all,

I'm trying to insert color from C++ to QML. I'm invoking a simple function

C++:

Q_INVOKABLE Color getColor() { return Color::fromARGB(0xfffd749e); }

QML:

Container {

    background: customColor.getColor()

}

 

but nothing happend. What i'm doing wrong? What object i should return from getColor() function?

 

Please use plain text.
Contributor
soulgen
Posts: 26
Registered: ‎02-11-2013
My Carrier: TELE2

Re: Color from C++ to QML

In additional I tryed to use JavaScrit function

function getColor(){
    return Color.Black;
}

Container {
    background: getColor()
}

But it doesn't work and i have no idea why? Any help?

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

Re: Color from C++ to QML

[ Edited ]

This should work

 

Container {
    background: getColor()

.................................

function getColor(){
    return Color.Black
}
}

Please use plain text.
Contributor
xtravanta
Posts: 45
Registered: ‎03-25-2011
My Carrier: KPN

Re: Color from C++ to QML

just wondering why not use the default color from qml?

background: Color.create("AARRGGBB")

Please use plain text.
Contributor
soulgen
Posts: 26
Registered: ‎02-11-2013
My Carrier: TELE2

Re: Color from C++ to QML

I need a function to get color from the name. This function should works not only within this container. The aim is prevent a lot of copy-paste code.

Please use plain text.
Developer
Zmey
Posts: 909
Registered: ‎12-18-2012

Re: Color from C++ to QML

Hi,

I tried to run the code from the first message and it works. The color changes.

 

This is what I've done:

 

In QML:

label.textStyle.color = app.getColor(); 

 

In Test.hpp:

#include <bb/cascades/Color>
...
class Test : public QObject
{
    Q_OBJECT
...
public:
    Q_INVOKABLE bb::cascades::Color getColor();
}

 In Test.cpp:

 

Test::Test(bb::cascades::Application *app)
: QObject(app)
{
    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

    qml->setContextProperty("app", this);
    AbstractPane *root = qml->createRootObject<AbstractPane>();
    app->setScene(root);
}

 ...

Color Test::getColor()
{
	return Color::fromARGB(0xfffd749e);
}

 

Make sure you're setting the qml context before creating the root object.

Please use plain text.
Contributor
soulgen
Posts: 26
Registered: ‎02-11-2013
My Carrier: TELE2

Re: Color from C++ to QML

Thanks, It's works for a single container! But I missed to say, that I tryed to invoke this function in ListItemComponent. And it still doesn't works :smileysad:

Please use plain text.
Contributor
nhuanvd
Posts: 12
Registered: ‎12-11-2012
My Carrier: Viettel

Re: Color from C++ to QML

You should add color code for every item to DataModel and use html and css for your text.

ListItemComponent {
	Label {
		text: "<html><span style=\"color: #" + ListItemData.textcolor + ";\">" + ListItemData.text + "</span></html>"
	}
}   

 

Please use plain text.