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.
void gui_1::on_pushButton_4_clicked()
{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);
gui_1 w;
w.show();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:
if(getColorBackgroung()==transparent){
setColot(green)
else
setColor(transparent)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
QPushButton {
background-color: red;
border-style: solid;
border-width: 3px;
border-radius: 75px 50px;
border-color: blue;
font: bold 14px;
padding: 6px;
}QPushButton:pressed {
background-color: rgb(224, 0, 0);
}QPushButton:checked {
background-color: rgb(0, 0, 224);
}you will have a push button that change color when checked
(note colors are random and butt ugly :)