c++ - char[4] 到 QColor 转换

标签 c++ qt colors qtgui qcolor

我有一个 char[4] 并且我希望能够将它转换成一个 QColor 有没有一种方法可以直接转换而不是这样做:

const char foo[4] = { 128, 128, 128, 128 };
const QColor bar( foo[0], foo[1], foo[2], foo[3] );

最佳答案

我假设你使用的是RGBA,所以你可以使用相应的构造函数:

QColor::QColor(int r, int g, int b, int a = 255)

Constructs a color with the RGB value r, g, b, and the alpha-channel (transparency) value of a.

The color is left invalid if any of the arguments are invalid.

您还可以使用以下静态方法:

QColor QColor::fromRgb(int r, int g, int b, int a = 255) [static]

Static convenience function that returns a QColor constructed from the RGB color values, r (red), g (green), b (blue), and a (alpha-channel, i.e. transparency).

All the values must be in the range 0-255.

关于c++ - char[4] 到 QColor 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24001664/

相关文章:

c# - 如何在 Windows 窗体 C# 中为文本框提供渐变颜色

c++ - 在 Qt 中获取耗时

c++ - 在工作线程中创建的 QObject 的线程亲和性会发生什么,然后终止?

c++ - clang 中的 constexpr 错误,但 gcc 中没有?

qt - 选择跨平台的 GUI 技术

qt - 从模型中删除的行仍在 View 中,我做错了什么?

c - 搜索 txt 文件然后在控制台打印 txt 文件并为搜索的字符串着色

PHP While - 每次都改变颜色

c++ - Arduino 与 Visual C++ 串口通信

c++ - 打印表达式树