Question

I am writing an Application using the Qt framework. I am using a custom CSS file to style my widgets and components like so:

  QPushButton:checked#switchButtonConsole, QPushButton:checked#switchButtonList, QPushButton:checked#switchButtonStats {                                                    
      background: qlineargradient(spread:reflect, x1:1, y1:0, x2:1, y2:1, stop:0 #151513, stop:1 #101010);                                                                    
      border-right: 1px solid #2E2E26;                                                                                                                                        
      padding: 5px;                                                                                                                                                           
      color: #94948E;                                                                                                                                                         
  } 

I have different CSS style depending on which state the button is in. Whether it is checked or unchecked.

I have 3 buttons and by default the first button is set to checked, the others are set to unchecked. Here is the code that I use to set the state of the button:

void MainWindow::on_switchButtonConsole_clicked() {                                                                                                                            
     ui->mainStack->setCurrentIndex(3);                                                                                                                                    

    if(!isChecked(ui->switchButtonConsole)) {                                                                                                                                  
        ui->switchButtonConsole->setChecked(true);                                                                                                                             
    };                                                                                                                                                                    

};

The problem I am having is that if I click on another button (remember, I have a function like the one above for each button) the previous button pressed will remain in the checked state and therefore the checked state style will remain. So if I click on all 3 buttons it looks like they are checked. I can't seem to figure out how to set the previous button pressed to the unchecked state, so that is only applies to checked state style to the button I CURRENTLY pressed and sets the unchecked style to the rest of the buttons.

Was it helpful?

Solution

The strait solution here is to add all three buttons to QButtonGroup and set it to exclusive. This way Qt will automagically do what you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top