PyQt5 - Setting skin to Checked CheckBox when pressed

PyQt5 - Setting skin to Checked CheckBox when pressed

To set a custom style to a checked QCheckBox when it's pressed, you'll again want to use Qt stylesheets. The QCheckBox has a few different sub-controls and pseudo-states you can utilize for styling.

For your requirement:

  • :checked refers to the checked state of the checkbox.
  • :pressed refers to when the checkbox is being pressed down by the user.

To style a checked QCheckBox when it's pressed, you would combine these two pseudo-states in your stylesheet.

Here's an example where the background of a checked and pressed QCheckBox is set to blue:

import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QCheckBox, QVBoxLayout, QWidget class CheckBoxDemo(QMainWindow): def __init__(self): super().__init__() self.central_widget = QWidget(self) self.setCentralWidget(self.central_widget) self.layout = QVBoxLayout(self.central_widget) self.checkbox = QCheckBox("Check me!", self) self.layout.addWidget(self.checkbox) self.initUI() def initUI(self): self.setWindowTitle("QCheckBox Custom Style") self.setGeometry(100, 100, 300, 200) # Styling the QCheckBox self.checkbox.setStyleSheet(""" QCheckBox:checked:pressed { background-color: blue; } """) self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = CheckBoxDemo() sys.exit(app.exec_()) 

In the above example, when you press down on a checked QCheckBox, the entire box and text will have a blue background. Adjust the stylesheet as needed to better fit your desired appearance.


More Tags

payara hive group-concat datetime-parsing flutter-animation identityserver4 scipy lodash filebeat bundler

More Programming Guides

Other Guides

More Programming Examples