如何使用十六进制值在 C++ 中设置标签颜色?
How can I set a label color in C++ using an hex value?
我正在使用 Momentics IDE(本机 SDK)开发 BlackBerry 10 移动应用程序。
我想要的是使用 TextStyleDefinition class 在 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));
我正在使用 Momentics IDE(本机 SDK)开发 BlackBerry 10 移动应用程序。
我想要的是使用 TextStyleDefinition class 在 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));