Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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_()
like image 335
alphanumeric Avatar asked Nov 14 '25 21:11

alphanumeric


1 Answers

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

like image 119
eyllanesc Avatar answered Nov 17 '25 12:11

eyllanesc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!