Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Setting QPushButton color wrong

    General and Desktop
    3
    4
    1762
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H
      houmingc 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 green

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

      }

      1 Reply Last reply Reply Quote 0
      • L
        LuGRU last edited by LuGRU

        Are Your push buttons checkable()? It seams like Your code should work, assuming checkable push buttons

        ui->pushButton->setCheckable( true);  
        
        1 Reply Last reply Reply Quote 0
        • H
          houmingc 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

          1 Reply Last reply Reply Quote 0
          • Chris Kawa
            Chris Kawa Moderators last edited by Chris Kawa

            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); }");
            
            1 Reply Last reply Reply Quote 0
            • First post
              Last post