简体   繁体   中英

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 .

Color::fromARGB() expects an integer, not a string...

Try that:

#include <cstdlib>
#include <iostream>
using namespace std;

int hexToInt(string s)
{
    char * p;
    if (s[0]=='#') s.replace(0,1,"");
    return (int)strtol(s.c_str(), &p, 16);
}

then

m_TSD->setColor(Color::fromARGB(hexToInt("#F01E21")));

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM