Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
How to know what's the current color background of a pushButton?
I put a pushButton like a toggle button and everytime a click(is. toggled), change the color to green and when I "distoggled" the color back to transparent.
QPalette palette; palette.setColor(ui.pushButton_4->backgroundRole(), Qt::red); ui.pushButton->setAutoFillBackground(true); ui.pushButton_4->setPalette(palette); ui.pushButton_4->setCheckable(true);
if I just do this the button will be toggled only in the second clicked because call first the click, to solve this I called the clicked method in the main:
int main(int argc, char *argv)
QApplication a(argc, argv);
w.on_pushButton_4_clicked(); return a.exec();
Hi and welcome to devnet,
Why not set the palette in the constructor of your widget ?
@SGaist why do I need this?
I'm new using Qt C++, so..
I could put the pushButton as a toggleButton, mas I need to change the color every time i toggle, but i couldn't, because I think I need to do something like:
something like that....
@SGaist I need to know how can i get the current color to be able change the color....
You rather need to know the state of your button. You should also be able to achieve this using style sheets
As SGaist is saying:
Using a style sheet is a bit easier as you do not need to keep track of state.
As I understand it, you want to have a different color when "checked" ?
If you right click a push button select "Change style sheet" and paste the following
border-radius: 75px 50px;
font: bold 14px;
background-color: rgb(224, 0, 0);
background-color: rgb(0, 0, 224);
you will have a push button that change color when checked
(note colors are random and butt ugly :)