Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Setting QPushButton color wrong
Forum Update on Monday, May 27th 2025

Setting QPushButton color wrong

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 2.2k Views
  • 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 Offline
    H Offline
    houmingc
    wrote on 31 May 2015, 01:20 last edited by houmingc
    #1

    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
    0
    • L Offline
      L Offline
      LuGRU
      wrote on 31 May 2015, 09:48 last edited by LuGRU
      #2

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

      ui->pushButton->setCheckable( true);  
      
      1 Reply Last reply
      0
      • H Offline
        H Offline
        houmingc
        wrote on 31 May 2015, 21:25 last edited by houmingc
        #3

        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
        0
        • C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 31 May 2015, 21:45 last edited by Chris Kawa
          #4

          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
          0

          4/4

          31 May 2015, 21:45

          • Login

          • Login or register to search.
          4 out of 4
          • First post
            4/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved