QCustomPlot Discussion and Comments

How to use QCPAxis::getPartAt(QPointF &pos) Return to overview

I have setup 12 charts in my App and had activated the Interaction Mode as follows:

for(int i=0; i<12; ++i) {
  customPlot[i]->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes);

When clicking the X axis on the 1st graph the selected axis will be highlighted in blue, which is good. When clicking the X axis on the 2nd graph now both X axes (1st and 2nd graph) are highlighted in blue. How do I overwrite this behavior so that only the last clicked axis is being highlighted (the 1st x axis should be deselected automatically). I had tried to find the current selected axis as follows but I end up getting both graphs.

QCustomPlot *selectedPlot;

for (int i=0; i<12; ++i) {
  if (customPlot[i]->xAxis->selectedParts().testFlag(QCPAxis::spAxis) ) {
    selectedPlot = customPlot[i];

Until then I found QCPAxis::getPartAt(QPointF &pos) from the API but I have no idea how to use it to solve this problem. Please help.

You could for example connect a slot with all axis' selectionChanged signal. In the slot you determine which axis emitted the signal (via qobject_cast<QCPAxis*>(sender()), see Qt documentation on signal/slot connections). Then, you go through all other axes and deselect them.

DenManu, thanks in the first place. Your hint also lead me to http://doc.qt.digia.com/qq/qq10-signalmapper.html

I thought the QCPAxis::getPartAt(QPointF &pos) does the "Signal Mapper Approach" by automatically giving me the closest Axis pointer based on where I clicked but too bad I still need to rely on something else to accomplish my initial goal.

Just out of curiousity. So the QCPAxis::getPartAt(QPointF &pos) is NOT what I think it is?


getPartAt is meant to return a QCPAxis::SelectablePart type. The selectable parts could be either spNone (objects out of the axis or manually set not to be selectable), spAxis (the axis line or its ticks excluding tick labels), spTickLabels (only the labels associated to the ticks) and spAxisLabel (the axis label f.e. X or Y).

This function does exactly what you need, but not automatically. You may make use of the spAxis return type to find the actual axis hit (so, not a related object like labels) by passing the mouse position as the function parameter of getPartAt. If the mouse is over the axis at the given point coordinates you may do your custom drawing.

I am unable to provide code as I don't have time and neither I ever tried to do accomplish this task, but if my logic is not broken it should work.