QCustomPlot scaling and re-positioning of all layers of the graph using the mouse

27 Views Asked by At

I write in qt using the qcustomplot library and qt5.15
There are n number of graphs created in one QCustomPlot object, I implement this through layout.

m_plot->plotLayout()->addElement(counter + offset, 0, axis);
m_plot->addGraphWithTracer(g, gp->element().label());
void CustomPlot::addGraphWithTracer(QCPGraph* graph, const QString& label)
{
    m_graphs.push_back(graph);
    m_labels.push_back(label);
    auto tracer = new CustomTracer(this);
    tracer->setGraph(graph);
    tracer->setGraphKey(5);
    tracer->setStyle(QCPItemTracer::tsNone);
    tracer->setInterpolating(true);
    tracer->setPen(QPen(Qt::red));
    tracer->setBrush(Qt::red);
    tracer->setSize(7);
    tracer->setClipToAxisRect(false);
    m_tracers.push_back(tracer);
}

I want to add the ability to scale and move the graph using the mouse. In the documentation, there is an implementation, but unfortunately, it only asks for the layout in which the mouse is located, and I need everything.

maybe someone knows how to trigger an event from other layers.

example of graphs:

enter image description hereenter image description here

documentation: https://www.qcustomplot.com/index.php/tutorials/userinteractions

customPlot->setInteraction(QCP::iRangeDrag, true)
customPlot->setInteraction(QCP::iRangeZoom, true)
1

There are 1 best solutions below

1
sQula On

Good afternoon, I found the answer to my question. QCustomPlot has a QCPAxis class, it has a rangeChange signal, it can be added to the lambda of your other graph, thus, when one graph changes, another one changes. Sample code:

for(int i = 0; i < QCustomPlot::axisRectCount(); i++)
        {
            for(int j = i+1; j < QCustomPlot::axisRectCount(); j++)
            {
                auto firstAxisRect = axisRect(i)->axes();
                auto secondAxisRect = axisRect(j)->axes();
                for(int k = 0; k < firstAxisRect.size(); k++)
                {
                    connect(
                        firstAxisRect[k],
                        QOverload<const QCPRange &>::of(&QCPAxis::rangeChanged),
                        [x = secondAxisRect[k]](
                            const auto& range){ x->setRange(range);});
                    connect(
                        secondAxisRect[k],
                        QOverload<const QCPRange &>::of(&QCPAxis::rangeChanged),
                        [x = firstAxisRect[k]](
                            const auto& range){ x->setRange(range);});
                }
            }
        }