Skip to main content

QCustomGradientPicker

QCustomGradientPicker

An editable multi-stop gradient control.

A gradient preview with draggable colour stops beneath it. Click the bar to add a stop, drag a handle to move it, double-click a handle to recolour it, Delete to remove it. Qt ships a colour dialog but nothing that edits a gradient, so anything needing one has had to hand-roll it.

Stops are held sorted by position and always number at least two - a "gradient" with one stop is a fill, and allowing it would make every consumer handle a degenerate case that has no reason to exist.

Alpha is preserved throughout and the preview is drawn over a checkerboard, so a translucent stop is visible as translucent rather than silently composited against whatever is behind the widget.

Emits gradientChanged(str) carrying the CSS-ish stop list, and stopSelected(int).

At a glance​

TierFree (GPLv3)
Importfrom Custom_Widgets.QCustomGradientPicker import QCustomGradientPicker
Qt DesignerYes — drag it from the palette

Quick start​

from Custom_Widgets.QCustomGradientPicker import QCustomGradientPicker

widget = QCustomGradientPicker()

Dark theme​

Colours come from the design tokens, so the widget follows the app theme with no extra work.

QCustomGradientPicker in dark theme

Properties​

Every property below is settable in code and in Qt Designer.

PropertyTypeDefault
stopsCsvstring0:#2563eb,1:#16a34a
gradientTypeenum: linear/radial``linear
angleint0
barHeightint28
handleRadiusint7
readOnlyboolFalse
stateenum: default/error``default
borderColorcolor—
borderActiveColorcolor—
borderErrorColorcolor—
handleColorcolor—
handleBorderColorcolor—

Signals​

Signal
gradientChanged(QString)
stopSelected(int)

Methods​

MethodDescription
addStop(position, colour=None)Insert a stop. Colour defaults to the gradient's colour there.
angle(*args, **kwargs)Angle.
barHeight(*args, **kwargs)Bar height.
borderActiveColor(*args, **kwargs)Border active color.
borderColor(*args, **kwargs)Border color.
borderErrorColor(*args, **kwargs)Border error color.
colorAt(position)Interpolated colour at a position, matching what the bar paints.
count()Count.
editStopColor(index=None)Open a colour dialog for a stop. Returns True if it changed.
gradient(rect=None)A QGradient over rect (defaults to the preview bar).
gradientChanged(...)Gradient changed.
gradientType(*args, **kwargs)Gradient type.
handleAt(point)Index of the stop handle under a point, or -1. Topmost wins.
handleBorderColor(*args, **kwargs)Handle border color.
handleColor(*args, **kwargs)Handle color.
handleRadius(*args, **kwargs)Handle radius.
readOnly(*args, **kwargs)Read only.
removeStop(index)Remove a stop. Refuses to go below MIN_STOPS.
selectedIndex()Selected index.
setSelectedIndex(index)Set the selected index.
setStopColor(index, colour)Set the stop color.
setStopPosition(index, position)Set the stop position.
setStops(stops)Replace every stop. Fewer than two valid stops is rejected.
state(*args, **kwargs)State.
stopColor(index)Stop the color.
stopPosition(index)Stop the position.
stopSelected(...)Stop the selected.
stops()[(position, QColor), ...] sorted by position.
stopsCsv(*args, **kwargs)Stops csv.

Theming​

Colours come from the design tokens, so they follow the active theme. Roles used: surface, on-surface, outline, focus-ring, destructive.

See Design tokens.

Runnable example​

A complete app using this widget lives at examples/PySide6/QCustomGradientPicker/main.py.

QCustomButtonGroup · QCustomCheckBox · QCustomColorPicker · QCustomComboBox · QCustomDateEdit · QCustomDateRangeEdit · QCustomDateRangePicker · QCustomEmojiPicker