Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

How not to lose the alpha channel

when converting between QRgb/QColor/QString

QColor toColor(QRgb colorWithAlpha)
{
    // all bad: input alpha ignored, set to fully opaque
    return QColor(colorWithAlpha);
    return colorWithAlpha;
    return QColor::fromRgb(colorWithAlpha);
    // good:
    return QColor::fromRgba(colorWithAlpha);
}
QString toString(const QColor &colorWithAlpha)
{
    // bad: input alpha ignored, output is e.g. #112233
    return colorWithAlpha.name();
    // good: output is e.g. #00112233
    return colorWithAlpha.name(QColor::HexArgb);
}

About KDAB


MilianWolff

Milian Wolff

Senior Software Engineer

Learn Modern C++

Learn more