Skip to content

The QPushButton within the QButtonGroup still retains its color when it is not selected. #110

@MeowSalty

Description

@MeowSalty

I tried using the following function to check the state when a QPushButton is clicked:

neko = ""

def buttonCat(button: QPushButton, group: QButtonGroup):
    global neko
    print(f"You clicked button {button.text()}")
    if neko == button.text():
        print("The button was clicked again.")
        group.setExclusive(False)
        for button in group.buttons():
            button.setChecked(False)
        group.setExclusive(True)
        neko = ''
    else:
        print("The button was clicked for the first time.")
        neko = button.text()

The main functionality of this function is to deselect the button when it is clicked again. Meanwhile, I'm using the following method to invoke the above function:

    # Create a QButtonGroup to manage mutually exclusive buttons.
    button_group = QButtonGroup(self)

    # Create H1 and H2 buttons.
    h1_button = QPushButton("H1")
    h1_button.setCheckable(True)
    h2_button = QPushButton("H2")
    h2_button.setCheckable(True)

    # Add the buttons to the button group.
    button_group.addButton(h1_button)
    button_group.addButton(h2_button)
    button_group.setExclusive(True)  # Set them to mutual exclusive mode, meaning only one button can be selected at a time.
    button_group.buttonClicked.connect(lambda button: buttonCat(button, button_group))

img

The behavior is normal when not using QSS.

img

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions