最近有需求,用到了Qwt只缩放单一方向的坐标轴,废话不多说 直接上代码。
由于是绑定按键的,直接放到了按键的槽函数上。
注:以下代码ui->qwtPlot_Temp_GetChange函数,为QwtPlot及曲线显示的子类信息
x轴放大:
void MainFrame::on_pushButton_X_Zoom_In_clicked()
{
const bool autoReplot = ui->qwtPlot_Temp_GetChange->autoReplot();
ui->qwtPlot_Temp_GetChange->setAutoReplot( false );
QwtInterval xs = ui->qwtPlot_Temp_GetChange->axisScaleDiv(QwtPlot::xBottom ).interval();
double xs_min = xs.minValue();
double xs_max = xs.maxValue();
double xs_differ = (xs_max - xs_min)/2;
ui->qwtPlot_Temp_GetChange->setAxisScale( QwtPlot::xBottom,
xs_min + xs_differ *0.1,
xs_max - xs_differ *0.1);
ui->qwtPlot_Temp_GetChange->setAutoReplot( autoReplot );
ui->qwtPlot_Temp_GetChange->replot();
}
x轴缩小:
void MainFrame::on_pushButton_X_Zoom_Out_clicked()
{
const bool autoReplot = ui->qwtPlot_Temp_GetChange->autoReplot();
ui->qwtPlot_Temp_GetChange->setAutoReplot( false );
QwtInterval xs = ui->qwtPlot_Temp_GetChange->axisScaleDiv(QwtPlot::xBottom ).interval();
double xs_min = xs.minValue();
double xs_max = xs.maxValue();
double xs_differ = (xs_max - xs_min)/2;
ui->qwtPlot_Temp_GetChange->setAxisScale( QwtPlot::xBottom,
xs_min - xs_differ *0.1,
xs_max + xs_differ *0.1);
ui->qwtPlot_Temp_GetChange->setAutoReplot( autoReplot );
ui->qwtPlot_Temp_GetChange->replot();
}
y轴放大:
void MainFrame::on_pushButton_Y_Zoom_In_clicked()
{
const bool autoReplot = ui->qwtPlot_Temp_GetChange->autoReplot();
ui->qwtPlot_Temp_GetChange->setAutoReplot( false );
QwtInterval ys = ui->qwtPlot_Temp_GetChange->axisScaleDiv(QwtPlot::yLeft ).interval();
double ys_min = ys.minValue();
double ys_max = ys.maxValue();
double ys_differ = (ys_max - ys_min)/2;
ui->qwtPlot_Temp_GetChange->setAxisScale( QwtPlot::yLeft,
ys_min + ys_differ *0.1,
ys_max - ys_differ *0.1);
ui->qwtPlot_Temp_GetChange->setAutoReplot( autoReplot );
ui->qwtPlot_Temp_GetChange->replot();
}
y轴缩小:
void MainFrame::on_pushButton_Y_Zoom_Out_clicked()
{
const bool autoReplot = ui->qwtPlot_Temp_GetChange->autoReplot();
ui->qwtPlot_Temp_GetChange->setAutoReplot( false );
QwtInterval ys = ui->qwtPlot_Temp_GetChange->axisScaleDiv(QwtPlot::yLeft ).interval();
double ys_min = ys.minValue();
double ys_max = ys.maxValue();
double ys_differ = (ys_max - ys_min)/2;
ui->qwtPlot_Temp_GetChange->setAxisScale( QwtPlot::yLeft,
ys_min - ys_differ *0.1,
ys_max + ys_differ *0.1);
ui->qwtPlot_Temp_GetChange->setAutoReplot( autoReplot );
ui->qwtPlot_Temp_GetChange->replot();
}
代码如上所示,希望对大家有所帮助!