I am trying to create toggle button in qt designer. I refer on internet also but i couldn\'t find how to do that. Can anyone know how to do toggle switch button. I have atta
A possible solution is to use a stylesheet with a QCheckBox. Just edit the stylesheet for the check box with the following code:
QCheckBox::indicator:unchecked {
image: url(switch_off.png);
}
QCheckBox::indicator:checked {
image: url(switch_on.png);
}
Minimal running example:
from PyQt5 import QtWidgets
import sys
app = QtWidgets.QApplication(sys.argv)
switch = QtWidgets.QCheckBox()
switch.setStyleSheet('''
QCheckBox::indicator:unchecked {
image: url(switch_off.png);
}
QCheckBox::indicator:checked {
image: url(switch_on.png);
}
''')
switch.show()
sys.exit(app.exec_())
Unfortunately, this doesn't always work well if you need some resizing and want the checkbox to adjust its appearance.
The only alternative is to subclass a QPushButton/QAbstractButton (with the checkable() property set to True) and implement the paintEvent on your own, as already suggested by the answer from eyllanesc.