Important: Please read the Qt Code of Conduct -

Set background color of QLabel using Qt Style Sheet and QColorDialog

  • I want to set the background of a label using Qt Style Sheet.
    I don't want to use QPalette.
    The user needs to select the color from the QColorDialog.
    I have set the function setColor() as a slot inside Class SelectColor.
    The variable rgba is declared as type QRgb inside the header file under private access.

    How can I pass in the variable rgba as argument for setStyleSheet() ?

    This is not working, it just set the background color to black:

    void SelectColor::setColor() {
        QColor color = QColorDialog::getColor(QColor(m_label->text()), this);
        m_label->setStyleSheet("background-color : rgba");


    #ifndef SELECTCOLOR_H
    #define SELECTCOLOR_H
    #include <QLabel>
    #include <QPushButton>
    #include <QVBoxLayout>
    #include <QHBoxLayout>
    #include <QPalette>
    #include <QColor>
    #include <QColorDialog>
    class QLabel;
    class SelectColor : public QWidget {
        SelectColor(QWidget *parent = 0);
    public slots:
        void setColor();
        // No signals
        QLabel *m_label;
        QRgb rgba;
    #endif // SELECTCOLOR_H

  • Hi JimmyMars ,

    Why not using a QString that combines the background part and the color . name () part

    something like this:

    QString str = " background-color : " ;
    str += color. name();

  • @Eddy
    But how do I change the background color using Qt Style sheet?
    I just don't know how to pass that rgba variable to that function.

  • Lifetime Qt Champion

    Hi its a little unclear what you ask about ?
    You can construct the stylesheet string as @Eddy shows.
    That will contain the color selected.

    • I just don't know how to pass that rgba variable to that function.
      What function are we talking about?

  • @JimmyMars

    QString str = " background-color : " ;
    QColor col (255,125,255);//takes rgb values
    str +=;

Log in to reply