qwtPlot实现清屏重绘

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Giser_D/article/details/85699437
#include "readflowpoint.h"
#include "qfile.h"
#include "qmessagebox.h"
#include "qwt_legend.h"
#include "qwt_plot_grid.h"
#include "qwt_scale_draw.h"
#include "qdatetime.h"
#include "qwt_series_data.h"
#include "qwt_plot_curve.h"

ReadFlowPoint::ReadFlowPoint(QWidget *parent, QString filepath)
	: QDialog(parent)
{
	ui.setupUi(this);
	ui.lineEdit_Time->setText(QDateTime::currentDateTime().toString("MM/dd/yyyy"));
	QList<QString> flowPoint_list = readFlowPointFile(filepath);
	writeFlowPointTable(flowPoint_list);//数据写入表格
	drawQwtPlotFlowPoint(flowPoint_list);//绘图
	connect(ui.lineEdit_Time, SIGNAL(editingFinished()), this, SLOT(dateChanged()));
}

void ReadFlowPoint::dateChanged()
{
	ui.qwtPlot_FlowPoint->detachItems(); //清除项
	timeJudgeList.clear();
	depthJudgeList.clear();
	QString timereceive = ui.lineEdit_Time->text();
	for (int i = 0; i < dateList.size(); i++)
	{
		if (dateList[i] == timereceive)
		{
			timeJudgeList.append(timeList[i]);
			depthJudgeList.append(depthList[i]);
		}
	}
	drawTidPointsPlot();
}

void ReadFlowPoint::setEnableAllAxis(bool yLeftEnable, bool yRightEnable, bool xBottomEnable, bool xTopEnable)
{
	ui.qwtPlot_FlowPoint->enableAxis(QwtPlot::yLeft, yLeftEnable);
	ui.qwtPlot_FlowPoint->enableAxis(QwtPlot::yRight, yRightEnable);
	ui.qwtPlot_FlowPoint->enableAxis(QwtPlot::xBottom, xBottomEnable);
	ui.qwtPlot_FlowPoint->enableAxis(QwtPlot::xTop, xTopEnable);
}

//将tidList数据转成点数据
QVector<QPointF> tidListConvertPoint(QStringList timeList, QStringList depthList) 
{
	QVector<QPointF> tidPointList;
	int counts = 0;
	QList<double> timeDoubleList,depthDoubleList;
	for (int i = 0; i < timeList.size(); i++)
	{
		counts++;
		QStringList xList = timeList[i].split(":");
		double mm = xList[0].toDouble();
		double sec = xList[1].toDouble();
		double secval = sec / 60;
		timeDoubleList.append(secval + mm);
	}
	for (int j = 0; j < depthList.size(); j++)
	{
		depthDoubleList.append(depthList[j].toDouble());
	}
	for (int k = 0; k < counts;k++)
	{
		QPointF tidPoint;
		tidPoint.setX(timeDoubleList[k]);
		tidPoint.setY(depthDoubleList[k]);
		tidPointList.append(tidPoint);
	}

	return tidPointList;
}

void ReadFlowPoint::drawTidPointsPlot()
{
	//设置坐标轴显示
	QVector<QPointF> tidPointList = tidListConvertPoint(timeJudgeList, depthJudgeList);
	setEnableAllAxis(true, false, true, true);
	//设置坐标轴刻度
	ui.qwtPlot_FlowPoint->setAxisScale(QwtPlot::xBottom, 0, 24, 1);
	ui.qwtPlot_FlowPoint->setAxisScale(QwtPlot::xTop, 0, 24, 1);
	//添加地图
	QwtPointSeriesData* series = new QwtPointSeriesData(tidPointList);
	QwtPlotCurve* curve1 = new QwtPlotCurve("时间\nCoor"); //设置line名称
	//设置数据
	curve1->setData(series);
	//把曲线附加到qwtPlot上
	curve1->attach(ui.qwtPlot_FlowPoint);
	curve1->setPen(QColor(255, 0, 0), 2, Qt::SolidLine);//设置画笔
	//添加格网
	QwtPlotGrid* grid = new QwtPlotGrid();
	grid->setPen(QColor(222, 222, 222), 1);
	grid->attach(ui.qwtPlot_FlowPoint);
	ui.qwtPlot_FlowPoint->insertLegend(new QwtLegend(), QwtPlot::BottomLegend);//添加图例
	ui.qwtPlot_FlowPoint->replot();
	ui.qwtPlot_FlowPoint->show();
}


void ReadFlowPoint::drawQwtPlotFlowPoint(QList<QString> flowPoint_list)
{
	//解析数据
	for (int i = 0; i < flowPoint_list.size(); i++)
	{
		QList<QString> flowPointDatas = flowPoint_list[i].split(" ");
		if (flowPointDatas.size() >= 3)
		{
			dateList.append(flowPointDatas[0]);
			timeList.append(flowPointDatas[1]);
			depthList.append(flowPointDatas[2]);
		}
	}
	ui.lineEdit_Time->setText(dateList[0]);   //设置默认数据日期为0;
	//绘图
	dateChanged();
}

QList<QString> ReadFlowPoint::readFlowPointFile(QString tid_path)  //return 验流点数据
{
	QList<QString> flowPoint_list;
	QFile file(tid_path);
	if (!file.open(QIODevice::ReadWrite|QIODevice::Text))
	{
		QMessageBox::warning(NULL, "文件打开", "文件打开失败");
	}
	while (!file.atEnd())
	{
		QByteArray line = file.readLine();
		QString str(line);
		flowPoint_list.append(str);
	}
	return flowPoint_list;
}

void ReadFlowPoint::writeFlowPointTable(QList<QString> flowPoint_list)
{
	//设置tableWidget表头内容
	ui.tableWidget_FlowPoint->setColumnCount(3);
	QStringList header;
	header.append(QString::fromLocal8Bit("日期"));
	header.append(QString::fromLocal8Bit("时间"));
	header.append("Corr");
	ui.tableWidget_FlowPoint->setHorizontalHeaderLabels(header);
	ui.tableWidget_FlowPoint->setRowCount(flowPoint_list.size());
	//设置宽度自适应
	ui.tableWidget_FlowPoint->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
	ui.tableWidget_FlowPoint->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
	//添加数据
	for (int i = 0; i < flowPoint_list.size(); i++)
	{
		QList<QString> flowPointDatas = flowPoint_list[i].split(" ");
		if (flowPointDatas.size() >= 3)
		{
			ui.tableWidget_FlowPoint->setItem(i, 0, new QTableWidgetItem(flowPointDatas[0]));
			ui.tableWidget_FlowPoint->setItem(i, 1, new QTableWidgetItem(flowPointDatas[1]));
			ui.tableWidget_FlowPoint->setItem(i, 2, new QTableWidgetItem(flowPointDatas[2]));
		}
	}
}

ReadFlowPoint::~ReadFlowPoint()
{
}

 

其中:::

最重要的是这句代码

    ui.qwtPlot_FlowPoint->detachItems(); //清除项

展开阅读全文

Qwtplot图像缩放显示不正确

12-07

开发环境:vs2008+Qt4.8.5+Qwt6.1.1rn问题描述:在显示图像时,初始显示是正确的,当进行放大然后再缩小时,比例尺不会返回到图像的初始状态,而是回到一个固定的状态,X轴,y轴均是0-1000。如下图所示:rn开始时:[img=https://img-bbs.csdn.net/upload/201412/07/1417925439_103052.jpg][/img]rn此时图像显示是正确的;rn点击右键缩放时:[img=https://img-bbs.csdn.net/upload/201412/07/1417925481_446743.jpg][/img]rn此时比例尺不再是原来的比例尺,而变成了0-1000,有人遇到这个问题么?rnrn相关代码的主要部分:rnmeasureDataCollect::measureDataCollect(QWidget *parent, Qt::WFlags flags)rn : QMainWindow(parent, flags)rnrn ui.setupUi(this);rnrn //图像区域rn ui.qwtPlot->setCanvasBackground( QColor( 29,100,141));rnrn //图例rn m_legend = new QwtLegend;rn m_legend->setDefaultItemMode( QwtLegendData::Checkable );rn ui.qwtPlot->insertLegend(m_legend,QwtPlot::BottomLegend );rnrn m_grid = new QwtPlotGrid();rn m_grid->attach(ui.qwtPlot);rnrn //缩放rn QwtPlotZoomer* zoomer = new QwtPlotZoomer( ui.qwtPlot->canvas() );rn zoomer->setMousePattern( QwtEventPattern::MouseSelect2,rn Qt::RightButton, Qt::ControlModifier );rn zoomer->setMousePattern( QwtEventPattern::MouseSelect3,rn Qt::RightButton );rnrn rn //比例尺rn ui.qwtPlot->setAxisScale( QwtPlot::xBottom, 0, 200 );rn ui.qwtPlot->setAxisScale( QwtPlot::yLeft, 400, 800 );rnrn QwtPlotCurve *curve = new QwtPlotCurve();rn curve->setTitle("11111" );rn /*curve->setOrientation( Qt::Vertical );*/rn QPolygonF points;rn int i=10;rn points << QPointF( 0+i*10, 300+i*10 ) << QPointF( 2.0+i*10, 400+i*10 )rn << QPointF( -(1.0+i*50), 312+i*10 ) << QPointF( 0.5+i*10, 446.8+i*10 )rn << QPointF( 4.0+i*10, 557.9+i*10 ) << QPointF( 5.0+i*10, 670.1+i*10 );rn curve->setSamples(points);rn curve->attach(ui.qwtPlot);rnrnrn请各位大神帮忙,已经调试了几天了,还是没有任何结果。。。。rn 论坛

没有更多推荐了,返回首页