#include #include #include "centralwidget.h" #include "filemanager.h" CentralWidget::CentralWidget(QWidget *parent) : QWidget(parent), _customPlot(new QCustomPlot) { setStyleSheet("background-color: lightgray;"); resize(888, 666); QHBoxLayout *layout = new QHBoxLayout(); layout->addWidget(_customPlot); this->setLayout(layout); // 创建两条竖线 _line1 = new QCPItemStraightLine(_customPlot); _line1->point1->setCoords(20, _customPlot->yAxis->range().lower); _line1->point2->setCoords(20, _customPlot->yAxis->range().upper); _line1->setPen(QPen(Qt::red)); _line1->setSelectable(true); _line2 = new QCPItemStraightLine(_customPlot); _line2->point1->setCoords(40, _customPlot->yAxis->range().lower); _line2->point2->setCoords(40, _customPlot->yAxis->range().upper); _line2->setPen(QPen(Qt::red)); _line2->setSelectable(true); // 安装事件过滤器 _eventHandler = new DragLineHandler(_customPlot, _line1, _line2, _graph,nullptr); _customPlot->installEventFilter(_eventHandler); #if 0 // init data QVector x(101), y(101); for (int i = 0; i < 101; i++) { x[i] = i / 50.0 - 1;//设置x的范围为-1~1 y[i] = x[i] * x[i]; } _customPlot->graph(0)->setData(x, y); //设置坐标轴标签 _customPlot->xAxis->setLabel("x coordinates"); _customPlot->yAxis->setLabel("y coordinates"); //设置坐标轴范围,以便我们可以看到全部数据 _customPlot->xAxis->setRange(0, 1); _customPlot->yAxis->setRange(0, 100); _customPlot->replot(); #endif // startTimer(1000); } CentralWidget::~CentralWidget() { FileManager::fileClose(); } void CentralWidget::setAnalysisMode(const CentralWidget::AnalysisMode) { } void CentralWidget::slotModeModify(const Global::Mode mode) { if (Global::Mode::ExperimentStart == mode) { // 创建画布,设置画布上的点数据 _graph = _customPlot->addGraph(); // 设置坐标轴标签 _customPlot->xAxis->setLabel("Temp/℃"); _customPlot->yAxis->setLabel("DSC/mW"); // 设置坐标轴范围,以便我们可以看到全部数据 _customPlot->xAxis->setRange(0, 400); _customPlot->yAxis->setRange(-20, 20); } else if (Global::Mode::Analysis == mode) { qDebug()<<"file close..."; FileManager::fileClose(); } } void CentralWidget::slotRecvCommonData(const CommonData &cd) { qDebug()<<"slotRevCommonData"; _customPlot->graph(0)->addData(cd.sample_temp, cd.dsc); // 添加数据到曲线 _customPlot->replot(); // FileManager::writeExperimentFile(cd); } void CentralWidget::slotRecvAnalysisFileName(const QString &fileName) { qDebug()<<"slotRecvAnalysisFileName"< dataVtr; FileManager::readExperimentFile(fileName,dataVtr); if(dataVtr.size() < 0){ return; } //判断界面上是不是有曲线,有的话先删除。 _customPlot->clearGraphs(); // 创建画布,设置画布上的点数据 _customPlot->addGraph(); // 设置坐标轴标签 _customPlot->yAxis->setLabel("DSC/mW"); _customPlot->xAxis->setLabel("Temp/℃"); // 设置坐标轴范围,以便我们可以看到全部数据 _customPlot->xAxis->setRange(0, 400); _customPlot->yAxis->setRange(-20, 20); QVector xVtr,yVtr; for(FileManager::ExperimentData &ed:dataVtr){ xVtr.push_back(ed.sampleTemp); yVtr.push_back(ed.dsc); } _customPlot->graph(0)->addData(xVtr,yVtr); _customPlot->replot(); } void CentralWidget::timerEvent(QTimerEvent *event) { // key的单位是秒 double key = QDateTime::currentDateTime().toMSecsSinceEpoch() / 1000.0; // 添加数据 // 使用随机数产生一条曲线 // double value0 = realDataI(); double value0 = QRandomGenerator::global()->bounded(10.123); _customPlot->graph(0)->addData(key, value0); // 添加数据到曲线 // 删除8秒之前的数据。这里的8要和下面设置横坐标宽度的8配合起来 // 才能起到想要的效果,可以调整这两个值,观察显示的效果。 _customPlot->graph(0)->data()->remove(key - 80); // 自动设定graph(1)曲线y轴的范围,如果不设定,有可能看不到图像 // 也可以用ui->customPlot->yAxis->setRange(up,low)手动设定y轴范围 _customPlot->graph(0)->rescaleValueAxis(true); // 这里的8,是指横坐标时间宽度为8秒,如果想要横坐标显示更多的时间 // 就把8调整为比较大到值,比如要显示60秒,那就改成60。 // 这时removeDataBefore(key-8)中的8也要改成60,否则曲线显示不完整。 _customPlot->yAxis->setRange(0, 20); // 设定x轴的范围 _customPlot->xAxis->setRange(key + 0.25, 80, Qt::AlignRight); // 设定x轴的范围 _customPlot->replot(); } void CentralWidget::analysisMode(const bool flag) { _line1->setVisible(flag); _line2->setVisible(flag); _eventHandler->setEnable(flag); #if 0 if(flag){ _line1->setVisible(true); _line2->setVisible(true); _eventHandler->setEnable(true); }else{ _line1->setVisible(true); _line2->setVisible(true); _eventHandler->setEnable(true); } #endif }