1、当绘制的线性图等需要水平拖动的时候(不用qwt里面的函数),可以用QSlider,代码如下
ui.horizontalSlider->setMaximum(1000); //需要拖动的越缓慢,平滑它的值就设置得越大 ui.horizontalSlider->setSingleStep(1); //拖动的最小单位 QObject::connect(ui.horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(sliderMovedSlot(int))); //在槽函数中改变qwt的横坐标显示范围 QObject::connect(ui.horizontalSlider, SIGNAL(sliderPressed()), this, SLOT(sliderPressSlot())); QObject::connect(ui.horizontalSlider, SIGNAL(sliderReleased()), this, SLOT(sliderReleaseSlot())); /*滑条按下*/ void RoadAllData::sliderPressSlot() { SLIDER_RUNNING = true; //滑条正在拖动,这个时候在添加曲线的点的地方,将不会改变曲线显示的范围(setRange) realLeft = ui.customPlot->xAxis->range().lower; //获取当前显示曲线的窗口的最小横坐标 realRight = ui.customPlot->xAxis->range().upper; //最大横坐标 } /*滑条释放*/ void RoadAllData::sliderReleaseSlot() { SLIDER_RUNNING = false; //滑条停止滑动 if (!FILEOPENING)//不是打开数据库,而是实时更新的状态 { ui.horizontalSlider->setValue(ui.horizontalSlider->maximum()); } } /*滑条滑动响应的槽函数*/ void RoadAllData::sliderMovedSlot(int value) { if (FILEOPENING) {
double min = graphData[0][0];
double max = graphData[0][graphData[0].size() - 1];
double left = 0, right = 0;//需要显示在界面的范围
double tmp = 0;
//获取left、right
if (UP_LINE_WAY)
{
tmp = value / (ui.horizontalSlider->maximum()*1.0) * (abs(max - min)) + min;
left = tmp;
right = tmp+0.3;
}
else
{
tmp = min - value / (ui.horizontalSlider->maximum()*1.0) * (abs(max - min));
left = tmp;
right = tmp - 0.3;
}
//qDebug() << left << right <<" "<< min << max;
//设置显示范围
ui.qwtPlot->xAxis->setRange(left, right);
ui.qwtPlot->replot();
}
}
2、实现qwt动态加载图片的时候,可以拖动水平滚动条查看历史数据(不受数据实时更新的影响)
else//实时 { //获取left、right if (UP_LINE_WAY) //上行,横坐标递增 { tmp = realRight - (ui.horizontalSlider->maximum() - value) / (ui.horizontalSlider->maximum()*1.0) * (realRight-realFirst);//realRight是指qwt曲线当前屏幕range的最大值,realFirst是指曲线的起点 right = tmp; left = tmp - 0.3; } else //下行,横坐标递减 { tmp = realLeft + (ui.horizontalSlider->maximum() - value) / (ui.horizontalSlider->maximum()*1.0) * (realFirst - realLeft);//realLeft指的是当前屏幕range的最大值。 right = tmp; left = tmp + 0.3; } qDebug() << "left:" << left << "right:" << right<<"value:"<<value; //设置显示范围 ui.customPlot->xAxis->setRange(left, right); ui.customPlot->replot(); } /*x轴设置反向或者正向*/ ui.customPlot->xAxis->setRangeReversed(true);//x轴反向 ui.customPlot->xAxis->setRangeReversed(false);//x轴正向