In QCustomPlot, if you want to "link scales" between two or more graphs so that, for example, their x-axes or y-axes are synchronized (zooming/panning on one plot updates the other automatically), you can share axes or connect their range changes.
The problem
You have multiple QCustomPlot instances or multiple graphs (QCPGraph) on the same plot and want their axes to stay in sync:
- When you zoom/pan one plot's axis, the others update automatically.
- This is typically done by sharing axis ranges.
Common solutions in QCustomPlot to link scales
- Sharing axes within the same plot
When you have multiple graphs on the same QCustomPlot widget, all those graphs share the same xAxis/yAxis by default. So zooming or panning on those axes affects all graphs.
If you want graphs on different axes (e.g. bottom and top or left and right), and you want the scales linked, you can link axes by sharing their range changes.
Example:
// Link the bottom axis and the top axis:
customPlot->xAxis2->setRange(customPlot->xAxis->range());
connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)),
customPlot->xAxis2, SLOT(setRange(QCPRange)));
// Similarly for left and right axis:
customPlot->yAxis2->setRange(customPlot->yAxis->range());
connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)),
customPlot->yAxis2, SLOT(setRange(QCPRange)));
- Linking axes between different QCustomPlot widgets
If you have two QCustomPlot widgets (say customPlot1 and customPlot2) and want to link their axes:
// Initially sync ranges
customPlot2->xAxis->setRange(customPlot1->xAxis->range());
customPlot2->yAxis->setRange(customPlot1->yAxis->range());
// Connect signals & slots so when customPlot1 axis changes, customPlot2 updates
connect(customPlot1->xAxis, &QCPAxis::rangeChanged,
customPlot2->xAxis, &QCPAxis::setRange);
connect(customPlot1->yAxis, &QCPAxis::rangeChanged,
customPlot2->yAxis, &QCPAxis::setRange);
// Optionally connect reverse if you want two-way syncing
connect(customPlot2->xAxis, &QCPAxis::rangeChanged,
customPlot1->xAxis, &QCPAxis::setRange);
connect(customPlot2->yAxis, &QCPAxis::rangeChanged,
customPlot1->yAxis, &QCPAxis::setRange);
- Make linked axes "follow" user interactions
Optionally, you need to disable the default context menu and axis interactions on the "slave" plot to avoid conflicts, or manage signals carefully to avoid infinite loops.
Full minimal example linking x-axis of two QCustomPlot widgets
// Assuming you have two QCustomPlot widgets: customPlot1, customPlot2
// Synchronize initially
customPlot2->xAxis->setRange(customPlot1->xAxis->range());
customPlot2->yAxis->setRange(customPlot1->yAxis->range());
// Connect range changed signals to keep in sync
connect(customPlot1->xAxis, &QCPAxis::rangeChanged,
customPlot2->xAxis, &QCPAxis::setRange);
connect(customPlot1->yAxis, &QCPAxis::rangeChanged,
customPlot2->yAxis, &QCPAxis::setRange);
// If you want two-way binding:
connect(customPlot2->xAxis, &QCPAxis::rangeChanged,
customPlot1->xAxis, &QCPAxis::setRange);
connect(customPlot2->yAxis, &QCPAxis::rangeChanged,
customPlot1->yAxis, &QCPAxis::setRange);
Notes:
-
When connecting two-way, watch for infinite signal loops. QCustomPlot's setRange usually internally checks if the range changed before emitting signals, so infinite loops typically do not happen, but if you see performance issues, consider disconnecting/reactivating signals or use some flag to ignore recursive updates.
-
You can link only xAxes or only yAxes if full sync not desired.
Summary
- To link scales in QCustomPlot, connect the axes'
rangeChanged(QCPRange) signals to the other axes' setRange() slots.
- For axes on the same plot, you can just share one axis or connect
xAxis and xAxis2.
- For separate plots, connect their axes signals with slots accordingly.
If you want, I can provide example code for a specific case — just let me know!