Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Initialization of QPalette.



  • Hello team,

    I was going through demo code of QPalette. Below is the code.
    QColor color = QColorDialog::getColor(QColor(m_label->text()),this,"Select color",QColorDialog::ShowAlphaChannel);
    if(color.isValid())
    {
    QPalette palette = m_label->palette();
    palette.setColor(QPalette::Window,color);
    m_label->setPalette(palette);
    m_label->setText(color.name());
    }

    Now instead of initializing QPalette like this QPalette palette = m_label->palette();
    if I do like this,
    QPalette palette;
    palette.setColor(QPalette::Window,color);
    m_label->setPalette(palette);
    m_label->setText(color.name());

    What is the difference in both initialization?



  • @ankit-thakar said in Initialization of QPalette.:

    What is the difference in both initialization?

    The first one grabs the current palette from m_label , sets the color to color and sets the edited palette to the label again.
    Everything else will be the same as m_labels palette.

    The second one creates a new palette with default parameters and color color (No additional brush, shadow or anything else)


Log in to reply