QPushButtons color



  • 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


  • Lifetime Qt Champion

    Hi,

    You can use the pseudo states :checked and :unchecked in your style sheet to set the button background colors





  • [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?


  • Lifetime Qt Champion

    The doc pointed by Tabi gives an example using the pressed state, it's the same for checked and unchecked



  • 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;");
    }
    @



  • 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
    
    
    }
    

    }



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.