c++blackberryqmlblackberry-10momentics

How can I set a label color in C++ using an hex value?


I'm developing a BlackBerry 10 mobile application using the Momentics IDE (native SDK).

All I want is to set a label color in c++ with an hex value using the TextStyleDefinition class like below :

Label* titleLabel = container->findChild<Label*>("titleLabelObj");

TextStyleDefinition* TSD;
TSD->setColor(Color::fromARGB("#F01E21"));

titleLabel->textStyle()->setBase(TSD()->style());

The problem is that the 'fromARGB(int argb)' fuction reclaim an int value so I tried to replace "#" by "0x" but it doesn't work.

Can any one help me on this ? I will be very thankfull .


Solution

  • Actually it was simple, you just need to precise the alpha ;

    // Let's take for example the hex color below :
    QString color = "#F01E21"
    
    // We need to convert string to int after we replace the "#" with "0x"
    bool ok;
    int stringColorToInt = color.replace("#", "0xFF").toUInt(&ok, 16) // The 'FF' is alpha
    
    // We set the color
    TSD->setColor(Color::fromARGB(stringColorToInt));