Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Create cursor #Qcustomplot



  • Hello all :)

    I would like to create a cursor that could indicate the points of a curve with the click of a mouse, and also to be able to move the cursor over the curve, even if it is possible to enter a value and the cursor goes to that value.

    I looked a bit at the QCPItemLine library (QCustomPlot *parentPlot) but I don't know if this is the right direction.

    For now I just have this piece of code to plot my graph:

     ui->plot->addGraph();
        ui->plot->graph(0)->setData(batch[i],batch1[i]);
        // give the axes some labels:
        ui->plot->xAxis->setLabel("batch 0");
        ui->plot->yAxis->setLabel("batch 1");
        // set axes ranges, so we see all data:
        ui->plot->xAxis->setRange(0, *std::max_element(batch[i].constBegin(), batch[i].constEnd())+1);
        ui->plot->yAxis->setRange(0, *std::max_element(batch1[i].constBegin(), batch1[etag].constEnd())+1);
        ui->plot->replot();
        ui->plot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom );
    
    

    if you have any advice or ideas, I'll take it!

    good day to all :)



  • for a static cursor you can use this code :

     bool ok=true;
    
           tracer_test=new QCPItemTracer(ui->graph5);
        // generate some data:
           QVector<double> x(101), y(101); // initialize with entries 0..100
           double k=0.5;
           for (int i=0; i<101; ++i)
           {
             x[i] = i/50.0 - 1; // x goes from -1 to 1
             y[i] = x[i]*x[i]; // let's plot a quadratic function
           }
           // create graph and assign data to it:
    
           test_QCPGraph=ui->graph5->addGraph();
           test_QCPGraph->setData(x,y);
           test_QCPGraph->setPen(QPen(Qt::blue));
           test_QCPGraph->rescaleKeyAxis();
    
           //ui->graph5->addGraph();
    
           ui->graph5->graph(0)->addData(x,y);
    
    
    
    
           // give the axes some labels:
           ui->graph5->xAxis->setLabel("x");
           ui->graph5->yAxis->setLabel("y");
           // set axes ranges, so we see all data:
           ui->graph5->xAxis->setRange(-1, 1);
           ui->graph5->yAxis->setRange(0, 1);
    
           tracer_test->setGraph(test_QCPGraph);
           tracer_test->setGraphKey(k);
           tracer_test->setInterpolating(ok);
           tracer_test->setStyle(tracer_test->tsCircle);
    
           ui->graph5->replot();
           ui->graph5->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables );
    

Log in to reply