Setting QPushButton color wrong
-
wrote on 31 May 2015, 01:20 last edited by houmingc
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)"); }
}
-
wrote on 31 May 2015, 09:48 last edited by LuGRU
Are Your push buttons checkable()? It seams like Your code should work, assuming checkable push buttons
ui->pushButton->setCheckable( true);
-
wrote on 31 May 2015, 21:25 last edited by houmingc
My code is set checkable(), just the color is not intended when it is uncheck.
Because it is using QGroupBox, it exhibits exclusive button select features.
When it is unselected, it's behavior is undetermined -
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); }");
4/4