Skip to main content

QCustomSwitch

QCustomSwitch screenshot

QCustomSwitch is an on/off toggle switch — a painted, iOS/Material-style sliding control with an animated thumb. Its colours come from the design tokens, it follows the active theme, and it comes in three sizes.


Overview

  • Painted toggle with a smooth animated thumb (off = left, on = right).
  • Three sizes via sizeVariant (sm / md / lg).
  • Tokenized colours (track on/off, thumb) that follow the theme.
  • Keyboard accessible — Space / Enter toggles when focused.
  • Emits toggled(bool).

Constructor

QCustomSwitch(parent=None, checked=False)
  • parent — the parent widget.
  • checked — initial state.

Properties

PropertyTypeDescription
checkedboolOn/off state.
sizeVariantstrsm / md / lg.
trackOnColor / trackOffColor / thumbColorQColorPainted colours (set from tokens via QSS qproperty-…).

Methods

MethodDescription
isChecked()Current state.
setChecked(checked)Set the state (animates); emits toggled on change.
toggle()Flip the state.

Signals

SignalDescription
toggled(bool)Emitted when the state changes.

Usage example

from qtpy.QtWidgets import QApplication, QWidget, QHBoxLayout, QLabel
from Custom_Widgets.QCustomSwitch import QCustomSwitch
from Custom_Widgets.JSonStyles.tokens import DesignTokens, applyDesignTokens

app = QApplication([])
applyDesignTokens(app, tokens=DesignTokens(theme="light"))

w = QWidget()
row = QHBoxLayout(w)
row.addWidget(QLabel("Enable notifications"))
sw = QCustomSwitch(checked=True)
sw.toggled.connect(lambda on: print("notifications:", "on" if on else "off"))
row.addWidget(sw)
w.show()
app.exec()

Theming

Styled from the design tokens (switch_qss in Custom_Widgets/JSonStyles/tokens.py), applied via applyDesignTokens — the same system every modern widget uses. The track/thumb colours come from the accent, outline, and surface roles. See Theming.

API reference

Generated from the widget's live metaobject — do not edit by hand.

Properties

PropertyTypeDefault
checkedboolFalse
sizeVariantenum: sm/md/lg``md
thumbPositionfloat
trackOnColorcolor
trackOffColorcolor
thumbColorcolor

Signals

Signal
toggled(bool)

Methods

MethodDescription
checked(*args, **kwargs)Checked.
isChecked()Return whether the widget is checked.
setChecked(checked)Set the checked.
sizeVariant(*args, **kwargs)Size variant.
thumbColor(*args, **kwargs)Thumb color.
thumbPosition(*args, **kwargs)Thumb position.
toggle()Toggle.
toggled(...)Toggled.
trackOffColor(*args, **kwargs)Track off color.
trackOnColor(*args, **kwargs)Track on color.