繁体   English   中英

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

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

我正在使用Momentics IDE(本机SDK)开发BlackBerry 10移动应用程序。

我想要的是使用TextStyleDefinition类在c ++中使用十六进制值设置标签颜色,如下所示:

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

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

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

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

谁可以帮我这个事 ? 我将非常感激。

Color :: fromARGB()需要一个整数,而不是一个字符串...

试试看:

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

然后

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

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