简体   繁体   English

如何在C ++中使用十六进制值设置标签颜色?

[英]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). 我正在使用Momentics IDE(本机SDK)开发BlackBerry 10移动应用程序。

All I want is to set a label color in c++ with an hex value using the TextStyleDefinition class like below : 我想要的是使用TextStyleDefinition类在c ++中使用十六进制值设置标签颜色,如下所示:

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. 问题是' fromARGB(int argb) '函数回收一个int值,因此我尝试将“ ”替换为“ 0x ”,但是它不起作用。

Can any one help me on this ? 谁可以帮我这个事 ? I will be very thankfull . 我将非常感激。

Color::fromARGB() expects an integer, not a string... Color :: fromARGB()需要一个整数,而不是一个字符串...

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 ; 其实很简单,您只需要精确调整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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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