QToggleSwitch¶
A checkable QAbstractButton
drawn as a toggle switch, with an optional text label. It's used like a
QCheckBox: isChecked(),
setChecked(), and the toggled signal.
Its colors and size are properties (onColor, offColor, handleColor,
switchWidth, switchHeight, and handleSize), so they can also be set in a
style sheet, as in QToggleSwitch { qproperty-onColor: red; }. For other
looks, subclass it and reimplement drawGroove, drawHandle, or drawText,
which are given a superqt.switch.QStyleOptionToggleSwitch with the colors and
sizes to use.
from qtpy.QtWidgets import QApplication
from superqt import QToggleSwitch
app = QApplication([])
switch = QToggleSwitch("Notifications")
switch.toggled.connect(print)
switch.show()
app.exec()
Subclass of QAbstractButton
QToggleSwitch inherits all of QAbstractButton's methods, signals, and properties. Only those that superqt adds or changes are listed here.
Properties¶
handleColor
¶
Color of the switch handle.
handleSize
¶
handleSize = Property(int, _get_handleSize, _set_handleSize)
Width/height of the switch handle.
offColor
¶
Color of the switch groove when it is off.
onColor
¶
Color of the switch groove when it is on.
Methods¶
animationDuration
¶
animationDuration() -> int
Return the duration of the animation in milliseconds.
drawGroove
¶
drawGroove(
painter: QPainter,
rect: QRectF,
option: QStyleOptionToggleSwitch,
) -> None
Draw the groove of the switch.
Parameters:
-
painter(QPainter) –The painter to use for drawing.
-
rect(QRectF) –The rectangle in which to draw the groove.
-
option(QStyleOptionToggleSwitch) –The style options used for drawing.
drawHandle
¶
drawHandle(
painter: QPainter,
rect: QRectF,
option: QStyleOptionToggleSwitch,
) -> None
Draw the handle of the switch.
Parameters:
-
painter(QPainter) –The painter to use for drawing.
-
rect(QRectF) –The rectangle in which to draw the handle.
-
option(QStyleOptionToggleSwitch) –The style options used for drawing.
drawText
¶
drawText(
painter: QPainter,
rect: QRectF,
option: QStyleOptionToggleSwitch,
) -> None
Draw the text next to the switch.
Parameters:
-
painter(QPainter) –The painter to use for drawing.
-
rect(QRectF) –The rectangle in which to draw the text.
-
option(QStyleOptionToggleSwitch) –The style options used for drawing.
initStyleOption
¶
initStyleOption(option: QStyleOptionToggleSwitch) -> None
Initialize the style option for the switch.