Search code examples
c++qtcolorsqtguiqcolor

char[4] to QColor Conversion


I have a char[4] and I want to be able to cast it into a QColor is there a way that I can directly convert rather than doing this:

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

Solution

  • I will assume that you are using RGBA, so you can use the corresponding constructor:

    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.

    You could also use the following static method:

    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.