PyQt5 - Skin to unchecked radio button when mouse hover it

PyQt5 - Skin to unchecked radio button when mouse hover it

To style the unchecked radio button when the mouse hovers over it, you can make use of the :unchecked and :hover pseudo-states with the QRadioButton's stylesheet.

Here's how to set the skin of an unchecked QRadioButton's indicator when the mouse hovers over it:

  • 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 hover over unchecked radio button */ QRadioButton::indicator:unchecked:hover { border: 2px solid gray; background-color: lightblue; /* 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 hover the mouse over an unchecked radio button, its indicator will change to a light blue color. You can adjust the background-color or any other style properties in the QRadioButton::indicator:unchecked:hover block of the stylesheet to achieve the desired appearance.


More Tags

cocoa-touch slack statsmodels file-handling asp.net-identity-3 git-log micro-optimization android-phone-call spark-streaming concurrency

More Programming Guides

Other Guides

More Programming Examples