Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wsndshx opened this issue Mar 14, 2024 · 0 comments

Comments

@wsndshx
Copy link

wsndshx commented Mar 14, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant