#include "form_spectrum.h" #include "ui_form_spectrum.h" #include "InstDevice.h" Form_Spectrum::Form_Spectrum(QWidget *parent) : QMainWindow(parent) ,ui(new Ui::Form_Spectrum) ,chart (new QChart() ) ,series(new QSplineSeries() ) ,axisX(new QValueAxis() ) ,axisY(new QValueAxis() ) ,bInited(false) { ui->setupUi(this); } //--------------------------------- Form_Spectrum::~Form_Spectrum() { delete ui; } //-------------------------------- void Form_Spectrum::InitChart() { series->clear(); series->append(0, 0); //*series << QPointF(300, 3000) << QPointF(400, 3000) << QPointF(500, 3000) << QPointF(600, 3000) << QPointF(800, 20000); //谱图显示主题 chart->setTheme(QChart::ChartThemeBlueIcy); chart->legend()->hide();//图列图标隐藏 chart->addSeries(series); chart->createDefaultAxes(); chart->setTitle("Spectrum"); chart->setTitleBrush(QColor(0, 49, 82));//普鲁士蓝 chart->layout()->setContentsMargins(3, 3, 5, 5);//设置外边界全部为0 chart->setMargins(QMargins(8, 20, 35, 8));//设置内边界 左 顶 右 底 chart->setBackgroundRoundness(10); //设置背景区域圆角 chart->setBackgroundBrush(QColor(205,255,255));//背景颜色 蒂芙尼 蓝 //chart->setBackgroundVisible(false); //去背景 //-----创建坐标------------------------------------- //axisX = new QValueAxis;//X轴 axisX->setRange(pXSIZE[0], pXSIZE[1]);//设置坐标轴范围 axisX->setTitleText("波长(nm)");//标题 axisX->setLabelFormat("%d"); //标签格式:每个单位保留几位小数 axisX->setTickCount(7); //主分隔个数:0到10分成20个单位 axisX->setMinorTickCount(1); //每个单位之间绘制了多少虚网线 一分为二 connect(axisX,SIGNAL(rangeChanged(qreal, qreal)),this,SLOT(mAyisX_rangeChanged(qreal, qreal))); //----------------------------------------------------------- axisYEvent = pYSIZE[1]; LaxisYmin = pYSIZE[0]; LenCount = (axisYEvent -LaxisYmin) /20; //----------------------------------------------------------- axisY->setRange(pYSIZE[0], pYSIZE[1] ); axisY->setTitleText("强度(Counts)"); axisY->setLabelFormat("%d"); //标签格式 axisY->setTickCount(7); axisY->setMinorTickCount(1);//一分为二 //axisX->setGridLineVisible(false); //------------------------------------------------------------------ axisY->setLinePenColor(QColor(Qt::darkBlue));//QColor(0, 120, 215) axisX->setLinePenColor(QColor(Qt::darkBlue));//QColor(0, 120, 215) // axisY->setGridLineVisible(true); //Y栅格是否显示 // axisX->setGridLineVisible(false);//X栅格是否显示 //------------------------------------------------------------------ //设置曲线的粗细及颜色; QPen glPen(QColor(0,47,167));// 普鲁士蓝Prussian Blue0, 49, 82; RGB (0,47,167)克莱因 蓝 ;128,0,32 glPen.setWidth(2); series->setPen(glPen); //-------------------------------------------- //为序列设置坐标轴 chart->setAxisX(axisX, series); chart->setAxisY(axisY, series); //QGraphicsView::DragMode //--------------------------------------------- ui->graphicsView->setChart(chart); ui->graphicsView->setRenderHint(QPainter::Antialiasing); //ui->graphicsView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); ui->graphicsView->setDragMode(QGraphicsView::DragMode::RubberBandDrag); } //----------------------------------------- void Form_Spectrum::rubberZoomAdapt(QRect viewportRect, QPointF fromScenePoint, QPointF toScenePoint) { QValueAxis *LaxisY = (QValueAxis*)(chart->axisY(series));//父类转子类 int interval_len = LaxisY->max() - LaxisY->min();//求当前的区间长度 LenCount = interval_len /20; axisYEvent = LaxisY->max(); LaxisYmin = LaxisY->min(); } //---------------------------------------- void Form_Spectrum::SetYRange(int *pY) { pYSIZE =pY; } //---------------------------------------- void Form_Spectrum::SetXRange(int *px) { pXSIZE =px; } //--------------------------------------- void Form_Spectrum::UpdateWindows( ) { if(pXSIZE[0]<200) pXSIZE[0]=200; if(pXSIZE[1]>800) pXSIZE[1]=800; if(pYSIZE[0]<0) pYSIZE[0]=0; if(pYSIZE[1]>60000) pYSIZE[1]=60000; axisX->setRange(pXSIZE[0], pXSIZE[1]);//设置坐标轴范围 axisY->setRange(pYSIZE[0], pYSIZE[1] ); //------------------------------------------- axisYEvent = pYSIZE[1]; LaxisYmin = pYSIZE[0]; LenCount = (axisYEvent -LaxisYmin) /20; } //-------------------------------------- void Form_Spectrum::wheelEvent(QWheelEvent *event) // 滚轮事件 { if(event->delta() > 0) { axisYEvent-=LenCount; // 当滚轮远离使用者时进行放大 if(axisYEvent60000) axisYEvent=60000; } axisY->setRange(LaxisYmin,axisYEvent); } //----------------------------------------------------- void Form_Spectrum::UpdateWindows(InstDevice *pClient) { this->pClient = pClient; points.clear(); int count = sizeof(pClient->m_fTrueWaveLength)/sizeof(pClient->m_fTrueWaveLength[0]); points.reserve(count); for(int i=0;im_fTrueWaveLength[i], pClient->rcvCmdStrenth.STRENTH[i])); } series->replace(points);//用replace 比append快 } //--------------------------------------------------- void Form_Spectrum::showEvent(QShowEvent *pEvent) { if(!bInited) { bInited = true; InitChart(); //鼠标点击拾取,显示 X波长 Y轴光强度值 //QObject::connect(series, &QSplineSeries::hovered, [=](const QPointF &point, bool state)mutable{ QObject::connect(series, &QSplineSeries::clicked, [=](const QPointF &point)mutable{ { QToolTip::showText(QCursor::pos(), QString("%1,%2").arg(point.x()).arg(qRound(point.y())), this); } }); //-------------------------------rubberBandChanged /* connect(ui->graphicsView,&QtCharts::QChartView::rubberBandChanged, //this, &Form_Spectrum::rubberZoomAdapt); */ //connect(ui->graphicsView, SIGNAL(QtCharts::QChartView::rubberBandChanged), this, SLOT(rubberZoomAdapt)); } } //----------------------------------------------------------- void Form_Spectrum::keyPressEvent(QKeyEvent *event) { switch (event->key()) { // 以下为新增 ui->graphicsView->setRubberBand(QChartView::RectangleRubberBand);//缩放 case Qt::Key_Escape: UpdateWindows(); break; case Qt::Key_F1: ui->graphicsView->setRubberBand(QChartView::NoRubberBand);// break; case Qt::Key_F2: ui->graphicsView->setRubberBand(QChartView::VerticalRubberBand);//缩放 break; case Qt::Key_F3: ui->graphicsView->setRubberBand(QChartView::HorizontalRubberBand);//缩放 break; case Qt::Key_F4: ui->graphicsView->setRubberBand(QChartView::RectangleRubberBand);//缩放 break; default: break; } } //---------------------------------------------------------------------------------------- void Form_Spectrum::on_graphicsView_rubberBandChanged(const QRect &viewportRect, const QPointF &fromScenePoint, const QPointF &toScenePoint) { rubberZoomAdapt(viewportRect,fromScenePoint,toScenePoint); } void Form_Spectrum::mAyisY_rangeChanged(qreal min, qreal max) { } void Form_Spectrum::mAyisX_rangeChanged(qreal min, qreal max) { }