LegendMarkers Example

The example shows how to make use of legend markers.

In this example we create an application which uses the QLegendMarker clicked signal to show/hide the corresponding series in a chart. The connect marker button connects a clicked signal from all markers in a legend to the handleMarkerClicked slot.

Running the Example

To run the example from Qt Creator, open the Welcome mode and select the example from Examples. For more information, visit Building and Running an Example.

Using Legend Markers

Our application has buttons for adding or removing a series in a chart and a button to connect or disconnect the legend markers clicked signal to our handler. In the image above we have connected the markers and clicked on one of them.

    // Connect all markers to handler
    const auto markers = m_chart->legend()->markers();
    for (QLegendMarker *marker : markers) {
        // Disconnect possible existing connection to avoid multiple connections
        QObject::disconnect(marker, &QLegendMarker::clicked,
                            this, &MainWidget::handleMarkerClicked);
        QObject::connect(marker, &QLegendMarker::clicked, this, &MainWidget::handleMarkerClicked);
    }

Here we connect the markers in the legend to our handler. To avoid connecting the same marker more than once, we first disconnect it.

    const auto markers = m_chart->legend()->markers();
    for (QLegendMarker *marker : markers) {
        QObject::disconnect(marker, &QLegendMarker::clicked,
                            this, &MainWidget::handleMarkerClicked);
    }

Here we disconnect all markers from our handler.

    QLegendMarker* marker = qobject_cast<QLegendMarker*> (sender());
    Q_ASSERT(marker);

In our handler we first cast the sender of the event to the QLegendMarker.

    switch (marker->type())

Then we check the type of the marker. This is required if we want to access the detailed methods of the marker and cast it to the correct type. If all we need is the pointer to QAbstractSeries, the casting isn't necessary. In case of a pie or bar series, we may need the pointer to a related QPieSlice or QBarSet.

        // Toggle visibility of series
        marker->series()->setVisible(!marker->series()->isVisible());

        // Turn legend marker back to visible, since hiding series also hides the marker
        // and we don't want it to happen now.
        marker->setVisible(true);

We want to toggle the visibility of the series, when the marker is clicked. To do so, we get the pointer to a related series from the marker and toggle its visibility. Since the legend marker follows the visibility of the series by default, we also set the marked back to visible. If we don't do so, the marker will be invisible in the legend and we can't click on it anymore.

        // Dim the marker, if series is not visible
        qreal alpha = 1.0;

        if (!marker->series()->isVisible())
            alpha = 0.5;

        QColor color;
        QBrush brush = marker->labelBrush();
        color = brush.color();
        color.setAlphaF(alpha);
        brush.setColor(color);
        marker->setLabelBrush(brush);

        brush = marker->brush();
        color = brush.color();
        color.setAlphaF(alpha);
        brush.setColor(color);
        marker->setBrush(brush);

        QPen pen = marker->pen();
        color = pen.color();
        color.setAlphaF(alpha);
        pen.setColor(color);
        marker->setPen(pen);

Instead of making the marker invisible when the series is hidden, we dim the color of the marker. Here we do it by modifying the color of the laberBrush.

Example project @ code.qt.io

© 2020 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.