QPushButtons color
-
wrote on 25 Oct 2013, 13:28 last edited by
Hey all,
I designed a Gui by the Qt Gui Designer and I have Toogle Buttons.
!http://abload.de/img/bildq2s1l.jpg(picture)!Now I want to say:
If the Toogle Button @setChecked(false) THEN Color = RED@
If the Toogle Button @setChecked(true) THEN Color = GREEN@I tryed to implement a function:
@void MainWindow::setFarbe_r() {
ui->pushButton->setStyleSheet("background-color:red;");@
and the Connect:
@connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(setFarbe_r()));@
but it doesnt work.What I also could imagine that under the button would be displayed a textline.
For example when the button is pressed -> text: "button is pressed"Thank you for Help
-
Hi,
You can use the pseudo states :checked and :unchecked in your style sheet to set the button background colors
-
wrote on 26 Oct 2013, 09:18 last edited by
I think you need "this":http://doc.qt.digia.com/4.7/stylesheet-examples.html#customizing-qpushbutton
HTH
-
wrote on 28 Oct 2013, 07:31 last edited by
[quote author="SGaist" date="1382729840"]Hi,
You can use the pseudo states :checked and :unchecked in your style sheet to set the button background colors[/quote]
Thanks for answer. But what do you mean? Can you explain your answer please.. Where shoud i use this pseudo states? And how?
Can you write an example please? -
The doc pointed by Tabi gives an example using the pressed state, it's the same for checked and unchecked
-
wrote on 7 Nov 2013, 08:15 last edited by
HI,
You just implement a slot through designer and follow your first method like this..
@ void MainWindow::setFarbe_r(bool checked) {
if(checked)
ui->pushButton->setStyleSheet("background-color:red;");
else
ui->pushButton->setStyleSheet("background-color:green;");
}
@ -
wrote on 7 Nov 2013, 10:16 last edited by
Hello zhekka777,
Why don't u use "toggled slot of QPushButton "provided by Qt in order to behave as a toggle button..I have used it to make a toggle button .
First drag and drop a QPushButton on MainWindow and set it's property "checkable" to true and go to slot of QPushButton choose signal toggled(bool).
Write the following snippet of code inside this slot.
void MainWindow::on_pushButton_toggled(bool checked)
{if(checked==false) { ui->pushButton->setStyleSheet("QPushButton{ background-color :red; }"); ui->pushButton->setText("Btn is not Pressed"); //optional }
else
{ ui->pushButton->setStyleSheet("QPushButton{ background-color :green; }"); ui->pushButton->setText("Btn is Pressed"); //optional }
}
-
wrote on 7 Nov 2013, 11:54 last edited by
pressed state is for mouse button pressed ...
the correct QSS for checked and unchecked(toggling states) are:
@QPushButton:on
{
background-color:rgba(255,0,0,255);
}QPushButton:off
{
background-color:rgba(0,0,255,255);
}@pushButton->setStyleSheet(the wole string in code above) or set it in designer