How to change a Button colour
Hello guys,
I searched everywhere for the answer to this question. But the answers I found are either too confusing or don’t work. I want a single serial code to change the color of a button while the window is running.
I have an OpenGL application, and I want the colour to be taken from a QColorDialog and then colour the background of my OpenGL scene, and meanwhile colour the button to show the user what colour chosen (I think you know what I mean).
Here’s the code I’m currently using. The code colours the background behind the button. I want the button itself to be coloured or to have a small section coloured inside it, like we see in all painting programs.
- backgroundColour->setRgb(0,0,0); //example colour, I could later manage to utilize this variable with a QColorDialog
- backgroundColourButton->setAutoFillBackground(true);
- backgroundColourButton->setPalette(Pal);
Thank you for any efforts :)
5 replies
Easiest way would be to use a style sheet on the button:
- backgroundColourButton->setStyleSheet("background-color: red");
BTW – according to a talk of Girish Ramakrishnan (one of the original developers of Qt widget style sheets) at DevDays 2006, it was exactly the need of “making a red push button” that led to the introduction of Qt style sheets eventually :-)
You must log in to post a reply. Not a member yet? Register here!


