Search code examples
pythonpyqtpyqt4qcheckbox

How to resize QCheckBox


The code below creates a single Dialog window with two checkboxes. Second checkbox was constrained to a 8x8px size with setMaximumSize(8, 8) function. But it appears that the smaller size of the checkbox widget was not applied to the cross icon. So the icon is clipped by the boundaries of the checkbox widget. How to make sure the cross icon is scaled proportionally with the checkbox widget?

enter image description here

from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])

panel=QtGui.QDialog()
panel.setLayout(QtGui.QVBoxLayout())

checkbox1 = QtGui.QCheckBox()
panel.layout().addWidget(checkbox1)

checkbox2 = QtGui.QCheckBox()
checkbox2.setMaximumSize(8, 8)
panel.layout().addWidget(checkbox2)

panel.show()
app.exec_()

Solution

  • In this case it is best to resize using the stylesheet:

    {your QCheckbox}.setStyleSheet("QCheckBox::indicator { width: npx; height: mpx;}")
    

    Complete code:

    import sys
    
    from PyQt4 import QtGui
    
    if __name__ == '__main__':
        app = QtGui.QApplication(sys.argv)
    
        panel = QtGui.QDialog()
        panel.setLayout(QtGui.QVBoxLayout())
    
        checkbox1 = QtGui.QCheckBox("normal1")
        panel.layout().addWidget(checkbox1)
    
        checkbox2 = QtGui.QCheckBox("small")
        checkbox2.setStyleSheet("QCheckBox::indicator { width: 10px; height: 10px;}")
        panel.layout().addWidget(checkbox2)
    
        checkbox1 = QtGui.QCheckBox("normal2")
        panel.layout().addWidget(checkbox1)
    
        panel.show()
        sys.exit(app.exec_())
    

    enter image description here