QCustomPlot和iRangeDrag在第二个右yAxis上

QCustomPlot and iRangeDrag on second right yAxis

本文关键字:yAxis 第二个 iRangeDrag QCustomPlot      更新时间:2023-10-16

我想让右轴可拖动

现在使用一个网站的例子,我可以使第一个yAxis可拖动双击它。

void MainWindow::mousePress()
{
// if an axis is selected, only allow the direction of that axis to be dragged
// if no axis is selected, both directions may be dragged
if (ui->customPlot->xAxis->selectedParts().testFlag(QCPAxis::spAxis))
    ui->customPlot->axisRect()->setRangeDrag(ui->customPlot->xAxis->orientation());
else if (ui->customPlot->yAxis->selectedParts().testFlag(QCPAxis::spAxis))
    ui->customPlot->axisRect()->setRangeDrag(ui->customPlot->yAxis->orientation());
else if (ui->customPlot->yAxis2->selectedParts().testFlag(QCPAxis::spAxis))
    ui->customPlot->axisRect()->setRangeDrag(ui->customPlot->yAxis2->orientation());
else
    ui->customPlot->axisRect()->setRangeDrag(Qt::Horizontal|Qt::Vertical);
}

我的图有2条线,每条线都有不同的yAxis。我想要实现的是在第二个(右边)yAxis(称为yAxis2)上实现相同的可拖动效果。下面的代码,即使我选择了yAxis2,它是yAxis被垂直拖动。

我猜问题是在axisRect(),这是只涉及到左边的yAxis,而不是他们两个。

我通过修改代码来解决这个问题,以便在右轴上也执行拖动调用。如果你看一下左轴上发生了什么,就会明白了。

我可以通过调用QCPAxisRect::setRangeDragAxes()yAxisyAxis2拖动到一起。

注意:如果你仍然希望xAxis用鼠标拖动,那么你需要把它也放在列表中,因为默认值将被覆盖。

QList<QCPAxis *> draggableAxes = {xAxis,yAxis,yAxis2};
myPlot->axisRect()->setRangeDragAxes(draggableAxes);

如果你想在缩放交互中也有类似的行为,也有一个setter !QCPAxisRect::setRangeZoomAxes()

相关文章: