0

The following code runs, but the checkbox is tiny (it should be large). Why doesn't this work as expected? (One can use styling to resize the text, but that's not what I'm trying to do here). I'll be grateful for any suggestions.

import sys

from PyQt5.QtWidgets import (
    QApplication,
    QCheckBox,
    QVBoxLayout,
    QWidget,
)

class Window(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        # Create a Checkbox with a text label:
        chkBox = QCheckBox(text="Check box if you want this option.")

        # Make the checkbox large (has no effect):
        chkBox.setStyleSheet('QCheckBox::indicator {width:24px; height:24px}')

        layout= QVBoxLayout()
        layout.addWidget(chkBox)
        self.setLayout(layout)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec())
3
  • It is possible that this element uses one of the current system bitmaps of fixed size, so width and height don't affect it. After all, up-scale resampling of a small bitmap cannot be done accurately. To solve this problem, you can mimic checkbox with your own control using different graphical element used to draw the check image. Commented Feb 10 at 3:39
  • 1. as the width and height docs clearly explain: "Unless otherwise specified, this property has no effect when set on widgets. If you want a widget with a fixed height, set the min-height and max-height to the same value"; 2. as also explained in the Sub-Controls docs: "With complex widgets [...] if one property or sub-control is customized, all the other properties or sub-controls must be customized as well". Commented Feb 10 at 3:58
  • 3. as a follow-up of point 2, changing properties of complex widgets (including its subcontrols) enforces the default "ugly" QCommonStyle (sometimes based on the QWindowsStyle), but the result may depend on the actual underlying style and the widget implementation. In general, if you want high customization of widgets while trying to retain the default style, QSS are not the choice. At the very least, use a QProxyStyle instead (which is fundamentally incompatible with QSS, by the way). Commented Feb 10 at 4:01

1 Answer 1

2

You need to add app.setStyle("fusion")

import sys
from PyQt5.QtWidgets import (
    QApplication,
    QCheckBox,
    QVBoxLayout,
    QWidget,
)


class Window(QWidget):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        # Create a Checkbox with a text label:
        chkBox = QCheckBox(
            text="Check box if you want this option.")

        # Make the checkbox large (has no effect):
        chkBox.setStyleSheet('''
            QCheckBox {
                spacing: 5px;
                font-size: 25px;                 
            }            
            QCheckBox::indicator {
                width: 33px; 
                height: 33px
            }
        ''')

        layout= QVBoxLayout(self)
        layout.addWidget(chkBox)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    
    app.setStyle("fusion")                           # !!! +++                         
    
    window = Window()
    window.show()
    sys.exit(app.exec())

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

This does indeed work, although I have no idea why.
@PhillipM.Feldman Because it uses another style. This also means that the appearance of all widgets will not look native, in case you're on Windows or macOS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.