02-21-2013 04:27 AM
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?
02-21-2013 04:50 AM
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?
02-21-2013 05:01 AM - edited 02-21-2013 05:05 AM
This should work
Container {
background: getColor()
.................................
function getColor(){
return Color.Black
}
}
02-21-2013 05:15 AM
just wondering why not use the default color from qml?
background: Color.create("AARRGGBB")
02-21-2013 05:46 AM
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.
02-22-2013 09:07 PM
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(th is);
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.
02-25-2013 03:26 AM
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 ![]()
02-27-2013 07:24 AM
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>"
}
}