[英]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.