PyQt5 QCommandLinkButton - Setting Border for Combined Checked and Pressed states

PyQt5 QCommandLinkButton - Setting Border for Combined Checked and Pressed states

If you want to set a border for a QCommandLinkButton in PyQt5 when it's in both the checked and pressed states simultaneously, you can make use of the Qt Stylesheets. You'll be using the :checked pseudostate to target the widget when it's checked and the :pressed pseudostate to target it when it's being pressed.

Here's how you can do this:

import sys from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget, QCommandLinkButton class App(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): layout = QVBoxLayout() # Create a QCommandLinkButton btn = QCommandLinkButton("Sample Button", self) btn.setCheckable(True) # Set a border for the button when it's both checked and pressed btn.setStyleSheet(""" QCommandLinkButton:checked:pressed { border: 3px solid red; } """) layout.addWidget(btn) self.setLayout(layout) self.setWindowTitle('QCommandLinkButton Border on Checked and Pressed') self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = App() sys.exit(app.exec_()) 

Here's the breakdown of the stylesheet:

  • QCommandLinkButton:checked:pressed: This selects the QCommandLinkButton when it's both checked and pressed. We set the border to be 3 pixels wide and solid red.

You can adjust the border settings in the stylesheet to fit your needs.


More Tags

session 32-bit poster mergesort np-complete imagemagick-convert limit fileapi wav oracle-apex

More Programming Guides

Other Guides

More Programming Examples