Skip to content

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.

QToggleSwitch QToggleSwitch
from qtpy.QtWidgets import QApplication

from superqt import QToggleSwitch

app = QApplication([])

switch = QToggleSwitch("Notifications")
switch.toggled.connect(print)
switch.show()

app.exec()

QToggleSwitch

QToggleSwitch(parent: QWidget | None = ...)
QToggleSwitch(
    text: str | None, parent: QWidget | None = ...
)

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

handleColor = Property(
    QtGui.QColor, _get_handleColor, _set_handleColor
)

Color of the switch handle.

handleSize

handleSize = Property(int, _get_handleSize, _set_handleSize)

Width/height of the switch handle.

offColor

offColor = Property(
    QtGui.QColor, _get_offColor, _set_offColor
)

Color of the switch groove when it is off.

onColor

onColor = Property(QtGui.QColor, _get_onColor, _set_onColor)

Color of the switch groove when it is on.

switchHeight

switchHeight = Property(
    int, _get_switchHeight, _set_switchHeight
)

Height of the switch groove.

switchWidth

switchWidth = Property(
    int, _get_switchWidth, _set_switchWidth
)

Width of the switch groove.

Methods

StyleOption

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.

setAnimationDuration

setAnimationDuration(msec: int) -> None

Set the duration of the animation in milliseconds.

To disable animation, set duration to 0.

QStyleOptionToggleSwitch

QStyleOptionToggleSwitch()

on_color

on_color = QtGui.QColor('#4D79C7')

off_color

off_color = QtGui.QColor('#909090')

handle_color

handle_color = QtGui.QColor('#d5d5d5')

switch_width

switch_width = 24

switch_height

switch_height = 12

handle_size

handle_size = 14

margin

margin = 2

text_alignment

text_alignment = (
    Qt.AlignmentFlag.AlignLeft
    | Qt.AlignmentFlag.AlignVCenter
)