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
soulgen
Posts: 44
Registered: ‎02-11-2013
My Device: Z10

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?

 

Developer
soulgen
Posts: 44
Registered: ‎02-11-2013
My Device: Z10

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?

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

Re: Color from C++ to QML

[ Edited ]

This should work

 

Container {
    background: getColor()

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

function getColor(){
    return Color.Black
}
}

Contributor
xtravanta
Posts: 46
Registered: ‎03-25-2011
My Device: Blackberry Z10

Re: Color from C++ to QML

just wondering why not use the default color from qml?

background: Color.create("AARRGGBB")

Developer
soulgen
Posts: 44
Registered: ‎02-11-2013
My Device: Z10

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.

Developer
Zmey
Posts: 1,523
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

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.


Andrey Fidrya, @zmeyc on twitter
Developer
soulgen
Posts: 44
Registered: ‎02-11-2013
My Device: Z10

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:

Developer
nhuanvd
Posts: 22
Registered: ‎12-11-2012
My Device: Blackberry Z10

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