Qt extension-QCustomPlot user interaction

I. Overview

QCustomPlot provides several built-in user interactions. They can be roughly divided into

  • Range operations by dragging with the mouse and rolling the mouse wheel
  • Select drawing entities by clicking on them
  • Signal emitted when the user clicks on the drawing entity

2. Operating scope

This is actually to enable the function of the mouse to pan up, down, left, right or zoom the Graph .

The default way for users to manipulate axis ranges is to perform a drag operation on the corresponding QCPAxisRect.
Insert image description here

To enable range dragging in a QCustomPlot control, the flag QCP::iRangeDrag needs to be added to the currently allowed interactions. This can be done with customPlot->setInteraction(QCP::iRangeDrag, true). To allow dragging in only one direction, use QCPAxisRect::setRangeDrag and specify Qt::Vertical or Qt::Horizontal. The default list allows both directions Qt::Vertical | Qt::Horizontal.

During a drag operation, axes configured via QCPAxisRect::setRangeDragAxes update their ranges in real time, automatically causing a redraw. This gives the user the impression of moving the drawing coordinate plane by grabbing with the mouse. Initially, configure the range drag axes as the bottom and left axes of the rectangle. For the default axis rectangles of the QCustomPlot control, they are QCustomPlot::xAxis and QCustomPlot::yAxis.

To change the size of the range, i.e. make the drawing larger or smaller, the user can use the mouse wheel. This behavior is controlled by the interaction flag QCP::iRangeZoom, which also needs to be activated via QCustomPlot::setInteraction. Just like range dragging, scaling can also be selected based on the affected axis and direction, see functions QCPAxisRect::setRangeZoomAxes and QCPAxisRect::setRangeZoom

. Additionally, the zoom strength can be controlled via QCPAxisRect::setRangeZoomFactor. On ordinary mouse hardware, one mouse wheel step corresponds to this factor applied to the axis range. If the factor is greater than 1, rolling the mouse wheel forward reduces the range (zooms in) and rolling the mouse wheel backward increases the range (zooms out). To reverse this behavior, set the mouse wheel scaling factor to less than 1 (but greater than zero). Zoom is always centered on the current mouse cursor position in the drawing.

This means that pointing the cursor at a feature of interest and rolling the mouse wheel will zoom in on that feature.

3. Selection mechanism

Insert image description here

QCustomPlot provides a selection mechanism that allows the user to select each component in the plot, such as axes and figures. You can use the interaction flags starting with QCP::iSelect(…) to control whether certain types of entities are generally selectable in the diagram. For example, setting customPlot->setInteraction(QCP::iSelectPlottables, true) will allow users to select plots (such as graphs) by clicking on them. Please see the QCP::Interaction documentation for all interaction flags.

To allow multiple objects to be selected simultaneously, the QCP::iMultiSelect interaction flag can be set. The user can then select multiple objects in succession by holding down the multi-selection modifier (see QCustomPlot::setMultiSelectModifier), which is Ctrl by default.

1. Control the selectability and selection status of Graph

Selectability can be further fine-tuned using the setSelectable function on individual objects. For example, if a specific graph in the drawing cannot be selected by the user, call thatGraph->setSelectable(false). The selected state can be modified programmatically through the setSelected function. Even if user selectability is disabled, the selection state can be changed programmatically.

To deselect all objects in the plot, call QCustomPlot::deselectAll.

2. Appearance of the selected object

Selected objects are often displayed with different pens, brushes, or fonts. In fact, it makes the selected object appear more prominently . This can be configured via the methods QCPGraph::setSelectedPen, QCPGraph::setSelectedBrush, QCPAxis::setSelectedLabelFont, QCPAxis::setSelectedBasePen, QCPItemText::setSelectedColor, just to name a few. As you can see, they are named similarly to the original (non-selected) properties, but with a "Selected" prefix.

3. Multipart objects

Some objects, such as axes and legends, have a more complex appearance, so a single Boolean value for selection is not sufficient. In these cases, both selectability and selection state are one or a combination of SelectablePart flags (the respective QFlags types are called SelectableParts). Each multipart object defines its own SelectablePart type.
Insert image description here

For example, QCPAxis conceptually consists of three parts: axis backbone with tick marks , tick labels (numbers) , and axis labels . Since these three parts should be individually selectable, qcpaaxis::SelectablePart defines qcpaaxis::spNone, qcpaaxis::spAxis, qcpaaxis::spTickLabels and qcpaaxis::spAxisLabel. To make the axis stems and tick labels selectable, but not the axis labels, call theAxis->setSelectableParts(qcpaaxis::spAxis|QCP::spTickLabels).

To control the current selection state of a multipart object, use the qcpaaxis::setSelectedParts method.

4. Respond to changes in choices

When the selection changes, each object emits a signal called selectionChanged. It doesn't matter whether the change was caused by the user or programmatically by calling setSelected/setSelectedParts.

If the selection in the plot is changed by user interaction, the QCustomPlot range signal QCustomPlot::selectionChangedByUser is emitted.

In the slot function connected to this signal, we can check the selection status of certain objects and react accordingly. Here, the QCustomPlot::selectedPlottables, selectedItems, selectedax, and selectedLegends methods may be useful for retrieving selected objects of a specific type.

4. User interaction signals

QCustomPlot is independent of the selection mechanism and emits various signals upon user interaction. The lowest level ones are QCustomPlot::mouseDoubleClick, mousePress, mouseMove, mouserrelease and mouseWheel signals. They are emitted when the corresponding events of the QCustomPlot control fire. Note that the cleanest approach is to create a subclass of QCustomPlot and reimplement the event methods (inherited from QWidget) with the same name. However, if we don't want to subclass QCustomPlot, these signals allow easier access to user interaction for simple tasks.

There are also more advanced signals that report clicks and double-clicks on certain objects in the plot: QCustomPlot::plottableClick, plottableDoubleClick, itemClick, itemDoubleClick, axisClick, axisDoubleClick, legendClick, legendDoubleClick, titleClick, and titleDoubleClick. All of these signals report which object was clicked (and which part in the case of a multipart object), and the associated QMouseEvent.

Guess you like

Origin blog.csdn.net/qq_43680827/article/details/133445779