PyQt5 - Skin of checked RadioButton indicator when pressed

PyQt5 - Skin of checked RadioButton indicator when pressed

To set the skin (i.e., appearance or style) of a QRadioButton's indicator when it's checked and pressed, you can use the QRadioButton's stylesheet. The :checked and :pressed pseudo-states combined will allow you to target this exact interaction.

Here's a simple example showing how to do this:

  • Setup & Imports:
import sys from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QRadioButton 
  • Create a Sample Window with a QRadioButton:
class MainWindow(QWidget): def __init__(self): super().__init__() layout = QVBoxLayout() radiobutton = QRadioButton("Sample Radio Button") layout.addWidget(radiobutton) self.setLayout(layout) # Apply the stylesheet radiobutton.setStyleSheet(""" /* Default style for unchecked state */ QRadioButton::indicator:unchecked { border: 1px solid gray; background-color: white; width: 16px; height: 16px; border-radius: 8px; /* half of width and height */ } /* Style for checked state */ QRadioButton::indicator:checked { border: 1px solid gray; background-color: green; width: 16px; height: 16px; border-radius: 8px; } /* Set a style for pressed indicator when radiobutton is checked */ QRadioButton::indicator:checked:pressed { border: 1px solid gray; background-color: blue; /* change this to suit your needs */ width: 16px; height: 16px; border-radius: 8px; } """) 
  • Run the Application:
app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_()) 

In this example, when you press a checked radio button, its indicator will change to a blue color. You can modify the background-color in the QRadioButton::indicator:checked:pressed block of the stylesheet to customize the appearance as you wish.


More Tags

imagemagick linear-gradients draw bitarray vs-unit-testing-framework argparse readonly aws-codebuild tcsh recordset

More Programming Guides

Other Guides

More Programming Examples