Search code examples
c++qtqpushbuttonqstylesheet

QPushButton background color dynamically


I have a QPushButton on which I would like to set the background color. I can use stylesheets for this:

m_iconButton->setStyleSheet("QPushButton {"
                            "background-color: red "
                            "}");

However I want to use this in a function void foo(const QColor& a_color). How would I make sure that the stylesheet takes this a_color as an argument?

So something like this:

void foo(const QColor& color) {
        m_iconButton->setStyleSheet("QPushButton {"
                                    "background-color: a_color"
                                    "}");
}

Is there a way to do this with stylesheets? And if not, how can I do this without them?


Solution

  • Something like this?

    void foo(const QColor& color) {
            m_iconButton->setStyleSheet(QString("QPushButton {"
                                                "background-color: %1"
                                                "}")
                                           .arg(color.name()));
    }