- Qt 6.11
- Qt Graphs
- C++ Classes
- QtGraphs
- QPieSlice
- Qt Graphs | Commercial or GPLv3

# QPieSlice Class

The QPieSlice class represents a single slice in a pie series. More...

Header: | `#include <QPieSlice>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Graphs)` `target_link_libraries(mytarget PRIVATE Qt6::Graphs)`
qmake: | `QT += graphs`
In QML: | PieSlice
Inherits: | QObject

- List of all members, including inherited members
- QPieSlice is part of Qt Graphs C++ Classes for 2D.

## Public Types

enum class | LabelPosition { Outside, InsideHorizontal, InsideTangential, InsideNormal }

## Properties

angleSpan : qreal borderColor : QColor borderWidth : qreal color : QColor explodeDistanceFactor : qreal exploded : bool label : QString labelArmLengthFactor : qreal labelColor : QColor labelFont : QFont | labelPosition : LabelPosition labelVisible : bool percentage : qreal sliceChildren : QQmlListProperty<QPieSlice> startAngle : qreal subSlicesCount : qsizetype subSlicesRatio : qreal subSlicesSum : qreal value : qreal

## Public Functions

| QPieSlice(QObject *parent = nullptr)
| QPieSlice(const QString &label, qreal value, QObject *parent = nullptr)
virtual | ~QPieSlice() override
qreal | angleSpan() const
bool | append(QPieSlice *slice)
bool | append(const QList<QPieSlice *> &slices)
QPieSlice * | append(const QString &label, qreal value)
QPieSlice * | at(qsizetype index) const
QColor | borderColor() const
qreal | borderWidth() const
QColor | color() const
qreal | explodeDistanceFactor() const
QPieSlice * | find(const QString &label) const
bool | insert(qsizetype index, QPieSlice *slice)
bool | isEmpty() const
bool | isExploded() const
bool | isLabelVisible() const
QString | label() const
qreal | labelArmLengthFactor() const
QColor | labelColor() const
QFont | labelFont() const
QPieSlice::LabelPosition | labelPosition()
qreal | percentage() const
bool | remove(QPieSlice *slice)
bool | remove(qsizetype index)
void | removeMultiple(qsizetype index, int count)
bool | replace(QPieSlice *oldSlice, QPieSlice *newSlice)
bool | replace(qsizetype index, QPieSlice *slice)
bool | replaceAll(const QList<QPieSlice *> &slices)
QPieSeries * | series() const
void | setBorderColor(QColor color)
void | setBorderWidth(qreal borderWidth)
void | setColor(QColor color)
void | setExplodeDistanceFactor(qreal factor)
void | setExploded(bool exploded)
void | setLabel(const QString &label)
void | setLabelArmLengthFactor(qreal factor)
void | setLabelColor(QColor color)
void | setLabelFont(const QFont &font)
void | setLabelPosition(QPieSlice::LabelPosition position)
void | setLabelVisible(bool visible = true)
void | setSubSlicesRatio(qreal subSlicesRatio)
void | setValue(qreal value)
QQmlListProperty<QPieSlice> | sliceChildren()
qreal | startAngle() const
QList<QPieSlice *> | subSlices() const
qsizetype | subSlicesCount() const
qreal | subSlicesRatio() const
qreal | subSlicesSum() const
bool | take(QPieSlice *slice)
qreal | value() const

## Public Slots

void | clear()

## Signals

void | angleSpanChanged()
void | borderColorChanged()
void | borderWidthChanged()
void | colorChanged()
void | explodeDistanceFactorChanged()
void | explodedChanged()
void | labelArmLengthFactorChanged()
void | labelChanged()
void | labelColorChanged()
void | labelFontChanged()
void | labelPositionChanged()
void | labelVisibleChanged()
void | percentageChanged()
void | startAngleChanged()
void | subSlicesAdded(const QList<QPieSlice *> &slices)
void | subSlicesCountChanged(qsizetype count)
void | subSlicesRatioChanged(qreal newSubSlicesRatio)
void | subSlicesRemoved(const QList<QPieSlice *> &slices)
void | subSlicesSumChanged(qreal sum)
void | valueChanged()

## Detailed Description

A pie slice has a value and a label. When the slice is added to a pie series, the QPieSeries object calculates the percentage of the slice compared with the sum of all slices in the series to determine the actual size of the slice in the graph.

By default, the label is hidden. If it is visible, it can be either located outside the slice and connected to it with an arm or centered inside the slice either horizontally or in parallel with the tangential or normal of the slice's arc.

By default, the visual appearance of the slice is set by a theme, but the theme can be overridden by specifying slice properties. However, if the theme is changed after the slices are customized, all customization will be lost.

To enable user interaction with the pie graph, some basic signals are emitted when users click pie slices or hover the mouse over them.

A pie slice can contain sub slices.

See also QPieSeries.

## Member Type Documentation

### enum class QPieSlice::LabelPosition

This enum describes the position of the slice label.

Constant | Value | Description
--- | --- | ---
`QPieSlice::LabelPosition::Outside` | `0` | The label is located outside the slice connected to it with an arm. This is the default value.
`QPieSlice::LabelPosition::InsideHorizontal` | `1` | The label is centered within the slice and laid out horizontally.
`QPieSlice::LabelPosition::InsideTangential` | `2` | The label is centered within the slice and rotated to be parallel with the tangential of the slice's arc.
`QPieSlice::LabelPosition::InsideNormal` | `3` | The label is centered within the slice and rotated to be parallel with the normal of the slice's arc.

## Property Documentation

### `[read-only]` angleSpan : qreal

This property holds the span of the slice in degrees. A full pie is 360 degrees, where 0 degrees is at 12 a'clock. Updated automatically once the slice is added to the series.

Access functions:

qreal | angleSpan() const

Notifier signal:

void | angleSpanChanged()

### borderColor : QColor

This property holds the color used to draw the slice border. This is a convenience property for modifying the slice.

Access functions:

QColor | borderColor() const
void | setBorderColor(QColor color)

Notifier signal:

void | borderColorChanged()

See also borderWidth.

### borderWidth : qreal

This property holds the width of the slice border. This is a convenience property for modifying the slice border width.

Access functions:

qreal | borderWidth() const
void | setBorderWidth(qreal borderWidth)

Notifier signal:

void | borderWidthChanged()

See also borderColor.

### color : QColor

This property holds the fill color of the slice. This is a convenience property for modifying the slice fill color.

Access functions:

QColor | color() const
void | setColor(QColor color)

Notifier signal:

void | colorChanged()

### explodeDistanceFactor : qreal

Determines how far away from the pie the slice is exploded.

- 1.0 means that the distance is the same as the radius.
- 0.5 means that the distance is half of the radius.

By default, the distance is 0.15

Access functions:

qreal | explodeDistanceFactor() const
void | setExplodeDistanceFactor(qreal factor)

Notifier signal:

void | explodeDistanceFactorChanged()

See also exploded.

### exploded : bool

This property holds whether the slice is separated from the pie.

Access functions:

bool | isExploded() const
void | setExploded(bool exploded)

Notifier signal:

void | explodedChanged()

See also explodeDistanceFactor.

### label : QString

This property holds the label of the slice.

Note: The string can be HTML formatted.

Access functions:

QString | label() const
void | setLabel(const QString &label)

Notifier signal:

void | labelChanged()

See also labelVisible, labelFont, and labelArmLengthFactor.

### labelArmLengthFactor : qreal

This property holds the length of the label arm. The factor is relative to the pie radius. For example:

- 1.0 means that the length is the same as the radius.
- 0.5 means that the length is half of the radius.

By default, the arm length is 0.15

Access functions:

qreal | labelArmLengthFactor() const
void | setLabelArmLengthFactor(qreal factor)

Notifier signal:

void | labelArmLengthFactorChanged()

See also label, labelVisible, and labelFont.

### labelColor : QColor

This property holds the color used to draw the slice label.

Access functions:

QColor | labelColor() const
void | setLabelColor(QColor color)

Notifier signal:

void | labelColorChanged()

### labelFont : QFont

This property holds the font used for drawing the label text.

Access functions:

QFont | labelFont() const
void | setLabelFont(const QFont &font)

Notifier signal:

void | labelFontChanged()

See also label, labelVisible, and labelArmLengthFactor.

### labelPosition : LabelPosition

This property holds the position of the slice label.

Access functions:

QPieSlice::LabelPosition | labelPosition()
void | setLabelPosition(QPieSlice::LabelPosition position)

Notifier signal:

void | labelPositionChanged()

See also label and labelVisible.

### labelVisible : bool

This property holds the visibility of the slice label. By default, the label is not visible.

Access functions:

bool | isLabelVisible() const
void | setLabelVisible(bool visible = true)

Notifier signal:

void | labelVisibleChanged()

See also label, labelFont, and labelArmLengthFactor.

### `[read-only]` percentage : qreal

This property holds the percentage of the slice compared to the sum of all slices in the series. The actual value ranges from 0.0 to 1.0. Updated automatically once the slice is added to the series.

Access functions:

qreal | percentage() const

Notifier signal:

void | percentageChanged()

See also value and QPieSeries::sum.

### `[read-only]` sliceChildren : QQmlListProperty<QPieSlice>

The list of sub slices contained within this slice. This is a default property that allows sub slices to be specified within a slice element in QML without explicitly using the children property name.

Access functions:

QQmlListProperty<QPieSlice> | sliceChildren()

### `[read-only]` startAngle : qreal

This property holds the starting angle of this slice in the series it belongs to. A full pie is 360 degrees, where 0 degrees is at 12 a'clock. Updated automatically once the slice is added to the series.

Access functions:

qreal | startAngle() const

Notifier signal:

void | startAngleChanged()

### `[read-only]` subSlicesCount : qsizetype

This property holds the number of sub slices in the slice.

Access functions:

qsizetype | subSlicesCount() const

Notifier signal:

void | subSlicesCountChanged(qsizetype count)

### subSlicesRatio : qreal

The relative size of the sub slices compared to the parent slice. The value ranges from 0.0 to 1.0, where 1.0 means that the sub slices take up the same amount of space as the parent slice would without sub slices. The default value is 0.6.

Access functions:

qreal | subSlicesRatio() const
void | setSubSlicesRatio(qreal subSlicesRatio)

Notifier signal:

void | subSlicesRatioChanged(qreal newSubSlicesRatio)

### `[read-only]` subSlicesSum : qreal

This property holds the sum of all sub slices.

The slice keeps track of the sum of all the sub slices it holds.

Access functions:

qreal | subSlicesSum() const

Notifier signal:

void | subSlicesSumChanged(qreal sum)

### value : qreal

This property holds the value of the slice.

Note: A negative value is converted to a positive value.

Access functions:

qreal | value() const
void | setValue(qreal value)

Notifier signal:

void | valueChanged()

See also percentage() and QPieSeries::sum().

## Member Function Documentation

### `[explicit]` QPieSlice::QPieSlice(QObject *parent = nullptr)

Constructs an empty slice with the parent parent.

See also QPieSeries::append() and QPieSeries::insert().

### QPieSlice::QPieSlice(const QString &label, qreal value, QObject *parent = nullptr)

Constructs an empty slice with the specified value, label, and parent.

See also QPieSeries::append() and QPieSeries::insert().

### `[override virtual noexcept]` QPieSlice::~QPieSlice()

Removes the slice. The slice should not be removed if it has been added to a series.

### `[invokable]` bool QPieSlice::append(QPieSlice *slice)

Appends the sub slice specified by slice to the slice. Sub slice ownership is passed to the slice.

Returns `true` if appending succeeds.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

### `[invokable]` bool QPieSlice::append(const QList<QPieSlice *> &slices)

Appends the array of sub slices specified by slices to the slice. Sub slice ownership is passed to the slice.

Returns `true` if appending succeeds.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

### `[invokable]` QPieSlice *QPieSlice::append(const QString &label, qreal value)

Appends a single sub slice with the specified value and label to the slice. Sub slice ownership is passed to the slice. Returns null if value is `NaN`, `Inf`, or `-Inf` and adds nothing to the series.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

### `[invokable]` QPieSlice *QPieSlice::at(qsizetype index) const

Returns the PieSlice at the position index. Returns null if no PieSlice was found.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

### `[slot]` void QPieSlice::clear()

Clears all sub slices from the slice.

### `[invokable]` QPieSlice *QPieSlice::find(const QString &label) const

Searches for a PieSlice which contains the label label. Returns the PieSlice if found, null otherwise.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

### `[invokable]` bool QPieSlice::insert(qsizetype index, QPieSlice *slice)

Inserts the sub slice specified by slice to the slice before the sub slice at the position specified by index. Sub slice ownership is passed to the slice.

Returns `true` if inserting succeeds.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

### bool QPieSlice::isEmpty() const

Returns `true` if the sub slices list is empty.

### `[invokable]` bool QPieSlice::remove(QPieSlice *slice)

Removes a single sub slice, specified by slice, from the slice and deletes it permanently.

The pointer cannot be referenced after this call.

Returns `true` if the removal succeeds.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

### `[invokable]` bool QPieSlice::remove(qsizetype index)

Removes the PieSlice at the location index. The PieSlice will be permanently deleted. Returns `true` if removing is successful.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

### `[invokable]` void QPieSlice::removeMultiple(qsizetype index, int count)

Removes multiple PieSlices from the slice starting from index to a number of count. The PieSlices will be permanently deleted.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

### `[invokable]` bool QPieSlice::replace(QPieSlice *oldSlice, QPieSlice *newSlice)

Replaces the PieSlice oldSlice with newSlice if found in the slice. oldSlice will be permanently deleted. Returns `true` if replacing is successful.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

### `[invokable]` bool QPieSlice::replace(qsizetype index, QPieSlice *slice)

Replaces the PieSlice at position index with the one specified by slice. The original PieSlice will be permanently deleted. Returns `false` if replacing any of the PieSlices fails.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

### `[invokable]` bool QPieSlice::replaceAll(const QList<QPieSlice *> &slices)

Replaces the entire list of PieSlices in the slice with the list specified by slices. All the original PieSlices will be permanently deleted. Returns `true` if all PieSlices are replaced successfully.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

### QPieSeries *QPieSlice::series() const

Returns the series that this slice belongs to.

See also QPieSeries::append().

### QList<QPieSlice *> QPieSlice::subSlices() const

Returns a list of sub slices that belong to this slice.

### `[signal]` void QPieSlice::subSlicesAdded(const QList<QPieSlice *> &slices)

This signal is emitted when the sub slices specified by slices are added to the slice.

See also append().

### qsizetype QPieSlice::subSlicesCount() const

Returns the number of the sub slices in this slice.

Note: Getter function for property subSlicesCount.

### `[signal]` void QPieSlice::subSlicesRemoved(const QList<QPieSlice *> &slices)

This signal is emitted when the sub slices specified by slices are removed from the slice.

See also remove().

### qreal QPieSlice::subSlicesSum() const

Returns the sum of all sub slices values in this slice.

Note: Getter function for property subSlicesSum.

See also QPieSlice::value(), QPieSlice::setValue(), and QPieSlice::percentage().

### `[invokable]` bool QPieSlice::take(QPieSlice *slice)

Takes a single sub slice, specified by slice, from the series. Does not delete the slice object.

Note: The slice remains the slice's parent object. You must set the parent object to take full ownership.

Returns `true` if the take operation was successful.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.