Setting QPushButton color wrong
-
My QButtonGroup is working, as only one Button in this group can be selected. But i had the problem with the color. I checked the code i never turn any pushbutton red but only in the SLOTS when it is clicked.
All initial QPushButton is green *right
Once QPushButton is selected, it is turn red *right
Once QPushButton is unselected, it turn red *wrong *i need the color to be greenI have alot of problem with this ButtonGroup class. I went into the extent Go to Slots: buttonReleased and set green color, but it is not working.
QButtonGroup* ButtonGroup = new QButtonGroup(this);
ButtonGroup->addButton(ui->pushButton11);
ButtonGroup->addButton(ui->pushButton12);
ButtonGroup->addButton(ui->pushButton13);
ButtonGroup->addButton(ui->pushButton14);
ButtonGroup->addButton(ui->pushButton21);
ButtonGroup->addButton(ui->pushButton22);
ButtonGroup->addButton(ui->pushButton23);
ButtonGroup->addButton(ui->pushButton24);ui->pushButton11->setAutoExclusive(true); ui->pushButton12->setAutoExclusive(true); ui->pushButton13->setAutoExclusive(true); ui->pushButton14->setAutoExclusive(true); ui->pushButton21->setAutoExclusive(true); ui->pushButton22->setAutoExclusive(true); ui->pushButton23->setAutoExclusive(true); ui->pushButton24->setAutoExclusive(true);
void MainWindow::on_pushButton11_clicked()
{
if(ui->pushButton11->isChecked())
{ui->pushButton11->setStyleSheet("background-color:rgb(255,0,0);color:rgb(0,0,255)"); } else { ui->pushButton11->setStyleSheet("background-color:rgb(0,255,0);color:rgb(255,0,0)"); }
}
-
QButtonGgroup is exclusive by default so setting
setAutoExclusive
on buttons belonging to it does nothing (they are already exclusive in that group).You are only changing colors for the clicked button. It will not affect the ones that get unchecked by group exclusion.
You should also avoid changing stylesheets on every click like that. Instead do it once and apply a separate stylesheet rule for checked and unchecked buttons, e.g.
QButtonGroup* ButtonGroup = new QButtonGroup(this); ButtonGroup->addButton(ui->pushButton11); ... setStyleSheet("QPushButton:checked { background-color:rgb(255,0,0); color:rgb(0,0,255); } \ QPushButton:!checked { background-color:rgb(0,255,0); color:rgb(255,0,0); }");