zoukankan      html  css  js  c++  java
  • C++ 给予QCustomPlot的编写

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
    
    
    
    
    
    
        srand(QDateTime::currentDateTime().toTime_t());
        ui->setupUi(this);
    
    
        //设置控件可以  接受拖拽
        setAcceptDrops(true);
    
    
        //修改线条粗细窗口
        style_line = new Style();
    
    
        //图形界面缩放
        ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes |
                                        QCP::iSelectLegend | QCP::iSelectPlottables);
        //x轴
        ui->customPlot->xAxis->setRange(0, 5100);
        //y轴
        ui->customPlot->yAxis->setRange(0, 1.05);
        //编程长方形
        ui->customPlot->axisRect()->setupFullAxesBox();
    
    
        //添加标题
        ui->customPlot->plotLayout()->insertRow(0);
        ui->customPlot->plotLayout()->addElement(0, 0, new QCPPlotTitle(ui->customPlot, "声音频率曲线"));
    
    
        // x轴
        ui->customPlot->xAxis->setLabel("频率");
        // y轴
        ui->customPlot->yAxis->setLabel("hz");
    
    
    
    
    
    
        ui->customPlot->legend->setVisible(true);
    
    
        QFont legendFont = font();
        legendFont.setPointSize(10);
        ui->customPlot->legend->setFont(legendFont);
        ui->customPlot->legend->setSelectedFont(legendFont);
        ui->customPlot->legend->setSelectableParts(QCPLegend::spItems); // legend box shall not be selectable, only legend items
    
    
    //    addRandomGraph();
    //    addRandomGraph();
    //    addRandomGraph();
    //    addRandomGraph();
    
    
    
    
        // connect slot that ties some axis selections together (especially opposite axes):
        connect(ui->customPlot, SIGNAL(selectionChangedByUser()), this, SLOT(selectionChanged()));
        // connect slots that takes care that when an axis is selected, only that direction can be dragged and zoomed:
        connect(ui->customPlot, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(mousePress()));
        connect(ui->customPlot, SIGNAL(mouseWheel(QWheelEvent*)), this, SLOT(mouseWheel()));
    
    
        // make bottom and left axes transfer their ranges to top and right axes:
        connect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->xAxis2, SLOT(setRange(QCPRange)));
        connect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->yAxis2, SLOT(setRange(QCPRange)));
    
    
        // connect some interaction slots:
        connect(ui->customPlot, SIGNAL(titleDoubleClick(QMouseEvent*,QCPPlotTitle*)), this, SLOT(titleDoubleClick(QMouseEvent*,QCPPlotTitle*)));
        connect(ui->customPlot, SIGNAL(axisDoubleClick(QCPAxis*,QCPAxis::SelectablePart,QMouseEvent*)), this, SLOT(axisLabelDoubleClick(QCPAxis*,QCPAxis::SelectablePart)));
        connect(ui->customPlot, SIGNAL(legendDoubleClick(QCPLegend*,QCPAbstractLegendItem*,QMouseEvent*)), this, SLOT(legendDoubleClick(QCPLegend*,QCPAbstractLegendItem*)));
    
    
        // connect slot that shows a message in the status bar when a graph is clicked:
        connect(ui->customPlot, SIGNAL(plottableClick(QCPAbstractPlottable*,QMouseEvent*)), this, SLOT(graphClicked(QCPAbstractPlottable*)));
    
    
        // setup policy and connect slot for context menu popup:  弹出菜单
        ui->customPlot->setContextMenuPolicy(Qt::CustomContextMenu);
        connect(ui->customPlot, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenuRequest(QPoint)));
    }
    
    
    MainWindow::~MainWindow()
    {
        delete ui;
    
    
    }
    
    
    void MainWindow::titleDoubleClick(QMouseEvent* event, QCPPlotTitle* title)
    {
      Q_UNUSED(event)
      // Set the plot title by double clicking on it
      bool ok;
      QString newTitle = QInputDialog::getText(this, "QCustomPlot example", "New plot title:", QLineEdit::Normal, title->text(), &ok);
      if (ok)
      {
        title->setText(newTitle);
        ui->customPlot->replot();
      }
    }
    
    
    void MainWindow::axisLabelDoubleClick(QCPAxis *axis, QCPAxis::SelectablePart part)
    {
      // Set an axis label by double clicking on it
      if (part == QCPAxis::spAxisLabel) // only react when the actual axis label is clicked, not tick label or axis backbone
      {
        bool ok;
        QString newLabel = QInputDialog::getText(this, "QCustomPlot example", "New axis label:", QLineEdit::Normal, axis->label(), &ok);
        if (ok)
        {
          axis->setLabel(newLabel);
          ui->customPlot->replot();
        }
      }
    }
    
    
    void MainWindow::legendDoubleClick(QCPLegend *legend, QCPAbstractLegendItem *item)
    {
      // Rename a graph by double clicking on its legend item
      Q_UNUSED(legend)
      if (item) // only react if item was clicked (user could have clicked on border padding of legend where there is no item, then item is 0)
      {
        QCPPlottableLegendItem *plItem = qobject_cast<QCPPlottableLegendItem*>(item);
        bool ok;
        QString newName = QInputDialog::getText(this, "QCustomPlot example", "折线名称:", QLineEdit::Normal, plItem->plottable()->name(), &ok);
        if (ok)
        {
          plItem->plottable()->setName(newName);
          ui->customPlot->replot();
        }
      }
    }
    
    
    void MainWindow::selectionChanged()
    {
      /*
       normally, axis base line, axis tick labels and axis labels are selectable separately, but we want
       the user only to be able to select the axis as a whole, so we tie the selected states of the tick labels
       and the axis base line together. However, the axis label shall be selectable individually.
    
    
       The selection state of the left and right axes shall be synchronized as well as the state of the
       bottom and top axes.
    
    
       Further, we want to synchronize the selection of the graphs with the selection state of the respective
       legend item belonging to that graph. So the user can select a graph by either clicking on the graph itself
       or on its legend item.
      */
    
    
      // make top and bottom axes be selected synchronously, and handle axis and tick labels as one selectable object:
      if (ui->customPlot->xAxis->selectedParts().testFlag(QCPAxis::spAxis) || ui->customPlot->xAxis->selectedParts().testFlag(QCPAxis::spTickLabels) ||
          ui->customPlot->xAxis2->selectedParts().testFlag(QCPAxis::spAxis) || ui->customPlot->xAxis2->selectedParts().testFlag(QCPAxis::spTickLabels))
      {
        ui->customPlot->xAxis2->setSelectedParts(QCPAxis::spAxis|QCPAxis::spTickLabels);
        ui->customPlot->xAxis->setSelectedParts(QCPAxis::spAxis|QCPAxis::spTickLabels);
      }
      // make left and right axes be selected synchronously, and handle axis and tick labels as one selectable object:
      if (ui->customPlot->yAxis->selectedParts().testFlag(QCPAxis::spAxis) || ui->customPlot->yAxis->selectedParts().testFlag(QCPAxis::spTickLabels) ||
          ui->customPlot->yAxis2->selectedParts().testFlag(QCPAxis::spAxis) || ui->customPlot->yAxis2->selectedParts().testFlag(QCPAxis::spTickLabels))
      {
        ui->customPlot->yAxis2->setSelectedParts(QCPAxis::spAxis|QCPAxis::spTickLabels);
        ui->customPlot->yAxis->setSelectedParts(QCPAxis::spAxis|QCPAxis::spTickLabels);
      }
    
    
      // synchronize selection of graphs with selection of corresponding legend items:
      for (int i=0; i<ui->customPlot->graphCount(); ++i)
      {
        QCPGraph *graph = ui->customPlot->graph(i);
        QCPPlottableLegendItem *item = ui->customPlot->legend->itemWithPlottable(graph);
        if (item->selected() || graph->selected())
        {
          item->setSelected(true);
          graph->setSelected(true);
    
    
        }
      }
    }
    
    
    void MainWindow::mousePress()
    {
      // if an axis is selected, only allow the direction of that axis to be dragged
      // if no axis is selected, both directions may be dragged
    
    
      if (ui->customPlot->xAxis->selectedParts().testFlag(QCPAxis::spAxis))
        ui->customPlot->axisRect()->setRangeDrag(ui->customPlot->xAxis->orientation());
      else if (ui->customPlot->yAxis->selectedParts().testFlag(QCPAxis::spAxis))
        ui->customPlot->axisRect()->setRangeDrag(ui->customPlot->yAxis->orientation());
      else
        ui->customPlot->axisRect()->setRangeDrag(Qt::Horizontal|Qt::Vertical);
    }
    
    
    void MainWindow::mouseWheel()
    {
      // if an axis is selected, only allow the direction of that axis to be zoomed
      // if no axis is selected, both directions may be zoomed
    
    
    
    
      if (ui->customPlot->xAxis->selectedParts().testFlag(QCPAxis::spAxis))
        ui->customPlot->axisRect()->setRangeZoom(ui->customPlot->xAxis->orientation());
      else if (ui->customPlot->yAxis->selectedParts().testFlag(QCPAxis::spAxis))
        ui->customPlot->axisRect()->setRangeZoom(ui->customPlot->yAxis->orientation());
      else
        ui->customPlot->axisRect()->setRangeZoom(Qt::Horizontal|Qt::Vertical);
    }
    
    
    
    
    
    
    
    
    void MainWindow::addRandomGraph()
    {
    
    
        /*
            添加随机折线
        */
      int n = 50; // number of points in graph
      double xScale = (rand()/(double)RAND_MAX + 0.5)*2;
      double yScale = (rand()/(double)RAND_MAX + 0.5)*2;
      double xOffset = (rand()/(double)RAND_MAX - 0.5)*4;
      double yOffset = (rand()/(double)RAND_MAX - 0.5)*5;
      double r1 = (rand()/(double)RAND_MAX - 0.5)*2;
      double r2 = (rand()/(double)RAND_MAX - 0.5)*2;
      double r3 = (rand()/(double)RAND_MAX - 0.5)*2;
      double r4 = (rand()/(double)RAND_MAX - 0.5)*2;
      QVector<double> x(n), y(n);
      for (int i=0; i<n; i++)
      {
        x[i] = (i/(double)n-0.5)*10.0*xScale + xOffset;
        y[i] = (qSin(x[i]*r1*5)*qSin(qCos(x[i]*r2)*r4*3)+r3*qCos(qSin(x[i])*r4*2))*yScale + yOffset;
    
    
        double xx = (i/(double)n-0.5)*10.0*xScale + xOffset;
        qDebug()<<"x:"<<xx;
    
    
        double yy = (qSin(x[i]*r1*5)*qSin(qCos(x[i]*r2)*r4*3)+r3*qCos(qSin(x[i])*r4*2))*yScale + yOffset;
        qDebug()<<"y:"<<yy;
        x[i] = xx*400;
        y[i] = yy/5;
    
    
      }
    
    
      ui->customPlot->addGraph();
    
    
      ui->customPlot->graph()->setName(QString("New graph %1").arg(ui->customPlot->graphCount()-1));
      ui->customPlot->graph()->setData(x, y);
      ui->customPlot->graph()->setLineStyle((QCPGraph::LineStyle)(rand()%5+1));
      if (rand()%100 > 75)
        ui->customPlot->graph()->setScatterStyle(QCPScatterStyle((QCPScatterStyle::ScatterShape)(rand()%9+1)));
      QPen graphPen;
    
    
      //创建一个随机颜色的指针变量  *color
      QColor *color = new QColor(rand()%245+10, rand()%245+10, rand()%245+10);
    
    
      //拿到新创建的这个线的对象、QCPGraph
      QCPGraph *graph = ui->customPlot->graph();
    
    
      //把以上两个变量、赋值给字典、line_colour
      line_colour.insert(graph,*color);
    
    
      //设置随机增加的线、随机颜色
      graphPen.setColor(*color);
    
    
      //设置随机的粗细
      //graphPen.setWidthF(rand()/(double)RAND_MAX*2+1);
    
    
      ui->customPlot->graph()->setPen(graphPen);
      ui->customPlot->replot();
    }
    
    
    
    
    
    
    void MainWindow::removeSelectedGraph()
    {
        /*
            删除当前选择的折线
        */
    
    
    //   line_model
      if (ui->customPlot->selectedGraphs().size() > 0)
      {
       //拿到当前折线
       QCPGraph *graph = ui->customPlot->selectedGraphs().first();
       //判断当前折线、是否在line_model当中
       if (line_model.keys().contains(graph)){
           //如果存在则拿到 这个线对应的model
           QString model = line_model[graph];
           //model_list删除当前的型号
           model_list.removeOne(model);
       }
        ui->customPlot->removeGraph(ui->customPlot->selectedGraphs().first());
        ui->customPlot->replot();
      }
    
    
    }
    
    
    void MainWindow::ChangeSelectedGraphStyle()
    {
        /*
            修改样式
        */
    
    
    
    
    
    
        //打印本来的线条颜色  (本来颜色的值存在字典当中 key是当前选择的线)
        qDebug()<<line_colour.value(ui->customPlot->selectedGraphs().first());
        QPen p;
    
    
    
    
        //如果选中线条、的宽度有值、则进去
        if(line_width.value(ui->customPlot->selectedGraphs().first())){
               //设置宽度
               p.setWidth(line_width.value(ui->customPlot->selectedGraphs().first()));
               //设置颜色
               p.setColor(line_colour.value(ui->customPlot->selectedGraphs().first()));
               ui->customPlot->selectedGraphs().first()->setPen(p);
    
    
        }
        //如果没有设置宽度、直接更改颜色
        else{
            p.setColor(line_colour.value(ui->customPlot->selectedGraphs().first()));
            ui->customPlot->selectedGraphs().first()->setPen(p);
        }
    
    
        ui->customPlot->selectedGraphs().first()->setLineStyle((QCPGraph::LineStyle)(rand()%5+1));
        if (rand()%100 > 75)
          ui->customPlot->selectedGraphs().first()->setScatterStyle(QCPScatterStyle((QCPScatterStyle::ScatterShape)(rand()%9+1)));
        ui->customPlot->replot();
    }
    
    
    
    
    
    
    
    
    void MainWindow::ChangeSelectedGraphCu()
    {
        /*
            修改曲线样式
        */
    
    
        //设置当前窗口无边框
    //    style_line->setWindowFlags(Qt::CustomizeWindowHint|Qt::FramelessWindowHint);//设置自定义无边框窗口;
    //    style_line->setWindowFlags(Qt::FramelessWindowHint);//设置自定义无边框窗口;
        //设置子窗口属性、 作用: 主窗口退出、子窗口同步退出.
        style_line->setWindowFlags(Qt::Sheet);
    
    
    
    
    
    
        //打卡修改样式界面
        style_line->show();
    
    
    //   连接自己写的匿名槽函数、在无参的时候 使用
    //    connect(style_line,SIGNAL(sendsignal(int)),this,[=](){
    
    
    //    });
    
    
        //槽函数
        connect(style_line,SIGNAL(sendsignal(int,int)),this,SLOT(recevie(int,int)));
    }
    
    
    
    
    
    
    void MainWindow::recevie(int num,int type_id){
        qDebug()<<"改变线条粗细、改变样式";
        QPen p;
        p.setWidth(num);
    
    
    
    
        //打印本来的线条颜色  通过字典的的key(当前选择的线对象graph(ui->customPlot->selectedGraphs()))、找到他所对应的颜色
        qDebug()<<line_colour.value(ui->customPlot->selectedGraphs().first());
    
    
        //把这个线的颜色、赋值给加粗后的线条
        p.setColor(line_colour.value(ui->customPlot->selectedGraphs().first()));
    
    
        //加粗选择的线条颜色
        ui->customPlot->selectedGraphs().first()->setPen(p);
    
    
        //记录加粗的线条
        line_width.insert(ui->customPlot->selectedGraphs().first(),num);
    
    
        //拿到这条线所需要的数据、可以生成excel、方便以后使用
    //    QCPDataMap* s = ui->customPlot->selectedGraphs().first()->data();
    
    
    
    
    
    
        //修改当前的样式
        if(type_id == 0){
            qDebug()<<"不改变";
            ui->customPlot->selectedGraphs().first()->setScatterStyle(QCPScatterStyle((QCPScatterStyle::ssNone)));
    
    
        }
        else if(type_id == 1)
        {
            ui->customPlot->selectedGraphs().first()->setScatterStyle(QCPScatterStyle((QCPScatterStyle::ssCross)));
        }
        else if(type_id == 2)
        {
            ui->customPlot->selectedGraphs().first()->setScatterStyle(QCPScatterStyle((QCPScatterStyle::ssPlus)));
        }
        else if(type_id == 3)
        {
            ui->customPlot->selectedGraphs().first()->setScatterStyle(QCPScatterStyle((QCPScatterStyle::ssCircle)));
        }
        else if(type_id == 4)
        {
            ui->customPlot->selectedGraphs().first()->setScatterStyle(QCPScatterStyle((QCPScatterStyle::ssDisc)));
        }
        else if(type_id == 5)
        {
            ui->customPlot->selectedGraphs().first()->setScatterStyle(QCPScatterStyle((QCPScatterStyle::ssTriangle)));
        }
    
    
        ui->customPlot->replot();
    
    
    
    
    }
    
    
    void MainWindow::ChangeSelectedGraphColour()
    {
        /*
            修改当前线条颜色  1、找到QT自带的颜色框
        */
        for (int i=0; i<ui->customPlot->graphCount(); ++i){
              QCPGraph *graph = ui->customPlot->graph(i);
              QCPPlottableLegendItem *item = ui->customPlot->legend->itemWithPlottable(graph);
              if (item->selected()/*||graph->selected()*/){
                QColorDialog *m_pColor = new QColorDialog(this);
    
    
                m_pColor->setWindowModality(Qt::ApplicationModal);
                m_pColor->setCurrentColor(QColor(Qt::red));//初始颜色
                m_pColor->show();
                m_pColor->move(720,200);
                connect(m_pColor,SIGNAL(currentColorChanged(QColor)),this,SLOT(ShowColor(QColor)));//显示当前选中颜色的效果
                connect(m_pColor,SIGNAL(colorSelected(QColor)),this,SLOT(SetColor(QColor)));//OK信号连接
              }
            }
    
    
    }
    
    
    
    
    void MainWindow::ShowColor(const QColor &color)//只要当前颜色在对话框中发生改变,就会触发该信号。
    {
        /*
            修改颜色
        */
        qDebug()<<"1111"<<color;
    }
    
    
    
    
    void MainWindow::Download_Data(){
        /*下载当前数据*/
        qDebug()<<"下载当前数据";
        QCPDataMap *QCPDataMap = ui->customPlot->selectedGraphs().first()->data();
    
    
        //创建路径path
        QString path = QFileDialog::getSaveFileName(this,"save","../","CSV(*.csv)");
        //创建迭代器
        QCPDataMap::iterator iter;
    
    
    
    
        if(path.isEmpty() == false)
        {
            QFile file; //创建文件对象
    
    
            //关联文件名字
            file.setFileName(path);
            bool isOk = file.open(QIODevice::WriteOnly);
            if(isOk == true)
            {
                QString title = "频率,测试细声系数
    ";
                file.write(title.toLocal8Bit());
                for(iter=QCPDataMap->begin();iter != QCPDataMap->end(); iter++){
                    if(iter.key() == 0){
                        continue;
                    }
                    //double转String并拼接字符串
                    QString data_t = QString::number(iter.key()) +","+ QString::number(iter->value) + "
    ";
                    file.write(data_t.toLocal8Bit());
                }
            }
            file.close();
        }
    
    
    
    
    }
    
    
    
    
    void MainWindow::SetColor(const QColor &color)//当用户选中某一颜色并点击“OK”后,就会触发该信号。
    {
        /*
            修改曲线颜色
    
    
            //设置曲线对应的颜色
            QMap<QCPGraph*,QColor> line_colour;
        */
    
    
    
    
        //把选择的线条和颜色作为字典的  key value赋值给line_colour
        line_colour.insert(ui->customPlot->selectedGraphs().first(),color);
    
    
        QPen p;
    
    
        //如果选中线条、的宽度有值、则进去
        if(line_width.value(ui->customPlot->selectedGraphs().first())){
               //设置宽度
               p.setWidth(line_width.value(ui->customPlot->selectedGraphs().first()));
               //设置颜色
               p.setColor(color);
               ui->customPlot->selectedGraphs().first()->setPen(p);
    
    
        }
        //如果没有设置宽度、直接更改颜色
        else{
            p.setColor(color);
            ui->customPlot->selectedGraphs().first()->setPen(p);
        }
    
    
        //刷新当前
        ui->customPlot->replot();
    }
    
    
    
    
    void MainWindow::removeAllGraphs()
    {
    /*
      删除所有的折线
    */
      ui->customPlot->clearGraphs();
      //删除list的所有数据
      model_list.clear();
      ui->customPlot->replot();
    
    
    }
    
    
    void MainWindow::contextMenuRequest(QPoint pos)
    {
      /*
            添加菜单selectedGraphs().first()->data();A
      */
    
    
      QMenu *menu = new QMenu(this);
      menu->setAttribute(Qt::WA_DeleteOnClose);
    
    
      if (ui->customPlot->legend->selectTest(pos, false) >= 0) // context menu on legend requested
      {
        menu->addAction("Move to top left", this, SLOT(moveLegend()))->setData((int)(Qt::AlignTop|Qt::AlignLeft));
        menu->addAction("Move to top center", this, SLOT(moveLegend()))->setData((int)(Qt::AlignTop|Qt::AlignHCenter));
        menu->addAction("Move to top right", this, SLOT(moveLegend()))->setData((int)(Qt::AlignTop|Qt::AlignRight));
        menu->addAction("Move to bottom right", this, SLOT(moveLegend()))->setData((int)(Qt::AlignBottom|Qt::AlignRight));
        menu->addAction("Move to bottom left", this, SLOT(moveLegend()))->setData((int)(Qt::AlignBottom|Qt::AlignLeft));
      }
      else  // general context menu on graphs requested
      {
        //右击显示 添加曲线、
        menu->addAction("添加随机曲线", this, SLOT(addRandomGraph()));
    
    
        //如果点击了曲线、那么显示删除当前的曲线
        if (ui->customPlot->selectedGraphs().size() > 0)
        {
    //        menu.addAction()
            menu->addAction("改变颜色", this, SLOT(ChangeSelectedGraphColour()));
            menu->addAction("修改样式", this, SLOT(ChangeSelectedGraphCu()));
            menu->addAction("删除当前曲线", this, SLOT(removeSelectedGraph()));
            menu->addAction("下载当前数据", this, SLOT(Download_Data()));
        }
    
    
        //删除全部的折线
        if (ui->customPlot->graphCount() > 0)
        {
            menu->addAction("删除全部曲线", this, SLOT(removeAllGraphs()));
        }
    
    
      }
    
    
      menu->popup(ui->customPlot->mapToGlobal(pos));
    }
    
    
    
    
    
    
    
    
    
    
    void MainWindow::moveLegend()
    {
      if (QAction* contextAction = qobject_cast<QAction*>(sender())) // make sure this slot is really called by a context menu action, so it carries the data we need
      {
        bool ok;
        int dataInt = contextAction->data().toInt(&ok);
        if (ok)
        {
          ui->customPlot->axisRect()->insetLayout()->setInsetAlignment(0, (Qt::Alignment)dataInt);
          ui->customPlot->replot();
        }
      }
    }
    
    
    void MainWindow::graphClicked(QCPAbstractPlottable *plottable)
    {
        /*
            点击graph线条
        */
      ui->statusbar->showMessage(QString("Clicked on graph '%1'.").arg(plottable->name()), 1000);
    }
    
    
    void MainWindow::dragEnterEvent(QDragEnterEvent * event) //拖动进入事件
    {
        qDebug()<<"拖动进入事件";
        if (event->mimeData()->hasUrls())  //数据中心是否包含URL
        {
            event->acceptProposedAction(); //如果是,接收
        }
        else
        {
            event->ignore();               //否则忽略
        }
    
    
    }
    
    
    void MainWindow::dropEvent(QDropEvent * event)                 //放下事件
    {
        QVector<double> arr_x(502), arr_y(502);
        qDebug()<<"放下事件"<<event->mimeData()->text();
        //获取路径
        QString file_path = event->mimeData()->text();
        //删除路径中不需要的
        file_path.replace("file:///","");
    
    
    
    
    
    
        //生成file对象
        QFile file(file_path);
        //只能读、如果失败直接返回
        if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            qDebug() << "Open Excel file failed!";
            return;
            QMessageBox::information(this,"失败","打开文件失败");
        }
        QString model;
    
    
        //生成list
        QStringList list;
        //清空list
        list.clear();
        //流式存储对象
        QTextStream in(&file);
        in.setCodec("GB2312");      //这行的目的是支持读取中文信息
            //遍历行、从第一行到最后一行
        for(int i = 0; !in.atEnd(); i++)
        {
            QString fileLine = in.readLine();
            list = fileLine.split(",", QString::SkipEmptyParts);    //根据","开分隔开每行的列
            if(i > 0)   //这里的目的是第一行的表项不赋值,如果需要表项内容可以去掉if
            {
                //遍历列,我的Domo只有4列
    //                for(int j = 0; j <= 4; j++)
    //                {
    //                    //list.at(j)就是每列的值
    //                }
                arr_x[i] = list[2].toDouble();
                arr_y[i] = list[3].toDouble();
                model = list[1];
                qDebug() << list;
            }
        }
        //关闭
        file.close();
        recevie_file(arr_x,arr_y,model);
    //    connect(this,SIGNAL(sendsignal(arr_x[i],arr_y[i])),this,SLOT(recevie(QVector<double>,QVector<double>)));
    }
    
    
    void MainWindow::recevie_file(QVector<double> arr_x,QVector<double> arr_y,QString model){
        //清楚之前的折现。
        //ui->customPlot->clearGraphs();
        qDebug()<<"进来啦";
    //    model_list.append(model);
    
    
        if(model_list.contains(model)){
           qDebug()<<"存在";
           QMessageBox::information(this,"错误","已经存在此型号");
           return;
        }
        else{
           qDebug()<<"不存在";
           model_list.append(model);
        }
    
    
        ui->customPlot->addGraph()->setName(model);
    
    
        QPen graphPen;
    
    
          //创建一个随机颜色的指针变量  *color
        QColor *color = new QColor(rand()%245+10, rand()%245+10, rand()%245+10);
    
    
          //拿到新创建的这个线的对象、QCPGraph
        QCPGraph *graph = ui->customPlot->graph();
    
    
          //把以上两个变量、赋值给字典、line_colour
        line_colour.insert(graph,*color);
    
    
          //设置随机增加的线、随机颜色
        graphPen.setColor(*color);
    
    
          //设置随机的粗细
         //graphPen.setWidthF(rand()/(double)RAND_MAX*2+1);
    
    
        //设置线条颜色
        graph->setPen(graphPen);
    
    
        //设置数据
        graph->setData(arr_x,arr_y);
    
    
        //设置当前线条的 型号
        line_model[graph] = model;
    
    
    
    
    //    QMap<QCPGraph*,Line_style*> Map_Line;
    
    
        //创建类去存储 折线数据   实例化自己的类
        Line_style *line_style_css = new Line_style();
        line_style_css->model_css = model;
        line_style_css->colour = *color;
        line_style_css->width = 1;
        line_style_css->style_css ="ssNone";
    
    
        Map_Line[graph] = line_style_css;
        qDebug()<<"机型:"<<Map_Line[graph]->model_css;
        qDebug()<<"颜色:"<<Map_Line[graph]->colour;
        qDebug()<<"宽:"<<Map_Line[graph]->width;
        qDebug()<<"样式:"<<Map_Line[graph]->style_css;
        //重画图像
        ui->customPlot->replot();
    
    
    }
    
    
    
    
    
    
    void MainWindow::on_pushButton_clicked()
    {
        /*拉布拉斯变化*/
       ui->customPlot->graph()->data();
    
    
       QCPDataMap *QCPDataMap = ui->customPlot->graph()->data();
    
    
    }
  • 相关阅读:
    linux十九压缩解压
    linux第十八dd命令
    【51单片机】数据类型
    【博客园】
    【C++】简介与环境的搭建
    【树莓派】安装TeamViewer
    【树莓派】Makefile的编写
    【cJSON库】cJSON库的使用
    【树莓派】忘记系统用户密码,如何重置密码
    【树莓派】树莓派与PC机通信
  • 原文地址:https://www.cnblogs.com/yuanjia8888/p/14948411.html
Copyright © 2011-2022 走看看