Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can I set the plot points through the mouse cursor?
Forum Updated to NodeBB v4.3 + New Features

How can I set the plot points through the mouse cursor?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 283 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Blackzero
    wrote on last edited by
    #1

    How to set up up and down on each plot point through the mouse cursor, I mean how the cursor can pull up and down on the plot point and get a value when the cursor pulls the point up or down.

    Screenshot 2024-06-18 111619.png

    #include "plot2d.h"
    #include "ui_plot2d.h"
    #include <QStringList>
    
    Plot2D::Plot2D(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::Plot2D)
    
    {
        ui->setupUi(this);
        Plot2D::makeplot();
    }
    
    Plot2D::~Plot2D()
    {
        delete ui;
    }
    
    void Plot2D::makeplot()
    {
    
        // Add new data points
        QVector<double> x2 = {0, 1, 2, 3, 4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29}; // Example x values
        QVector<double> y2 = {1000
                              ,1200
                              ,1400
                              ,1500
                              ,1600
                              ,1650
                              ,1700
                              ,1800
                              ,1900
                              ,2000
                              ,2250
                              ,2500
                              ,2750
                              ,3000
                              ,3250
                              ,3500
                              ,3750
                              ,4000
                              ,4500
                             , 5000
                              ,5500
                              ,6000
                              ,6500
                              ,7000
                              ,7500
                              ,8000
                              ,8500
                              ,9000
                              ,11500}; // Given y values
    
        QCPGraph *graph2 = ui->customPlot->addGraph();
        graph2->setData(x2, y2);
        graph2->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssSquare, QPen(Qt::red, 1.5), QBrush(Qt::red), 9));
        graph2->setPen(QPen(QColor(200, 50, 50), 2));
    
        // move bars above graphs and grid below bars:
        ui->customPlot->addLayer("abovemain", ui->customPlot->layer("main"), QCustomPlot::limAbove);
        ui->customPlot->addLayer("belowmain", ui->customPlot->layer("main"), QCustomPlot::limBelow);
        graph2->setLayer("abovemain");
        ui->customPlot->xAxis->grid()->setLayer("belowmain");
        ui->customPlot->yAxis->grid()->setLayer("belowmain");
    
        // set some pens, brushes and backgrounds:
        ui->customPlot->xAxis->setBasePen(QPen(Qt::white, 1));
        ui->customPlot->yAxis->setBasePen(QPen(Qt::white, 1));
        ui->customPlot->xAxis->setTickPen(QPen(Qt::white, 1));
        ui->customPlot->yAxis->setTickPen(QPen(Qt::white, 1));
        ui->customPlot->xAxis->setSubTickPen(QPen(Qt::white, 1));
        ui->customPlot->yAxis->setSubTickPen(QPen(Qt::white, 1));
        ui->customPlot->xAxis->setTickLabelColor(Qt::white);
        ui->customPlot->yAxis->setTickLabelColor(Qt::white);
        ui->customPlot->xAxis->grid()->setPen(QPen(QColor(140, 140, 140), 1, Qt::DotLine));
        ui->customPlot->yAxis->grid()->setPen(QPen(QColor(140, 140, 140), 1, Qt::DotLine));
        ui->customPlot->xAxis->grid()->setSubGridPen(QPen(QColor(80, 80, 80), 1, Qt::DotLine));
        ui->customPlot->yAxis->grid()->setSubGridPen(QPen(QColor(80, 80, 80), 1, Qt::DotLine));
        ui->customPlot->xAxis->grid()->setSubGridVisible(true);
        ui->customPlot->yAxis->grid()->setSubGridVisible(true);
        ui->customPlot->xAxis->grid()->setZeroLinePen(Qt::NoPen);
        ui->customPlot->yAxis->grid()->setZeroLinePen(Qt::NoPen);
        ui->customPlot->xAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
        ui->customPlot->yAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
        QLinearGradient plotGradient;
        plotGradient.setStart(0, 0);
        plotGradient.setFinalStop(0, 350);
        plotGradient.setColorAt(0, QColor(80, 80, 80));
        plotGradient.setColorAt(1, QColor(50, 50, 50));
        ui->customPlot->setBackground(plotGradient);
        QLinearGradient axisRectGradient;
        axisRectGradient.setStart(0, 0);
        axisRectGradient.setFinalStop(0, 350);
        axisRectGradient.setColorAt(0, QColor(80, 80, 80));
        axisRectGradient.setColorAt(1, QColor(30, 30, 30));
        ui->customPlot->axisRect()->setBackground(axisRectGradient);
    
        // Adjust axes to fit the new data
        ui->customPlot->rescaleAxes();
        ui->customPlot->yAxis->setRange(*std::min_element(y2.constBegin(), y2.constEnd()), *std::max_element(y2.constBegin(), y2.constEnd()));
        ui->customPlot->replot();
    }
    
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You should start with the user interactions tutorials from the QCustomPlot documentation.

      For further help, you should check with the QCustomPlot community as they will likely be more knowledgeable about this module.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved