Search code examples
c++qtcolorsalphaqcolor

Converting QColor into QString without losing alpha value


Consider the code:

QColor m_color = QColor(255,255,255,0); //alpha is 0
QString m_str = m_color.name(); //#ffffff

m_color is a transparent color with alpha value 0. Now I want to convert this color value into QString without losing transparency (alpha value).

QColor.name() converts it to #ffffff which is 'white' color whereas I want transparent color. What should I do?


Solution

  • Simple answer: Just use the second overload of QColor::name:

    QString m_str = m_color.name(QColor::HexArgb); //#00ffffff
    

    Note: Please check the Qt documentation first for such questions - it is barely worth asking on STOF...