【问题标题】:How can I set a label color in C++ using an hex value?如何使用十六进制值在 C++ 中设置标签颜色?
【发布时间】:2015-04-15 10:25:39
【问题描述】:

我正在使用 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 值,所以我尝试替换 " #" by "0x" 但它不起作用。

任何人都可以帮助我吗?我会很感激的。

【问题讨论】:

标签: c++ blackberry qml blackberry-10 momentics


【解决方案1】:

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

【讨论】:

  • 当它是一个解析问题并且返回值是一个像 1234567 这样的 int 类型时它可以工作,但是当它在 fromARGB() 函数中使用它时它什么都不做并且标签将是不可见的并且在编译器中不显示任何东西。 – J.M.J 27 分钟前
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    • 2011-04-07
    • 2014-07-03
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    相关资源
    最近更新 更多