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. Overriding QCustomPlot library method with your own method
Forum Update on Monday, May 27th 2025

Overriding QCustomPlot library method with your own method

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 2 Posters 703 Views
  • 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.
  • L Offline
    L Offline
    lukutis222
    wrote on 4 Apr 2024, 05:42 last edited by
    #1

    I am working with QCustomPlot on the QT to write my application. This question is related to QCustomPlot but more related to C++ in general hence I have decided to ask here.

    QCustomPlot source files can be found here:
    https://www.qcustomplot.com/index.php/download

    I want to slightly change the funcionality of the QCustomPlot by overriding some methods (mousePressEvent and mouseMoveEvent) to achieve simillar funcionality as described here: https://www.qcustomplot.com/index.php/support/forum/1887

    So I have tried to create a custom class named QMyPlot and made it a subclass of QCPAxisRect.

    See my QMyPlot.h:

    #ifndef QMYPLOT_H
    #define QMYPLOT_H
    
    #include "qcustomplot.h"
    
    class QMyPlot : public QCPAxisRect
    {
    public:
        QMyPlot();
    
    };
    
    #endif // QMYPLOT_H
    
    

    and QMyPlot.cpp:

    #include "qmyplot.h"
    
    QMyPlot::QMyPlot()
    {
    
    }
    
    
    

    I am getting an error:

    C:\Users\my_user\Desktop\WORK\QT\uCurrent\uCurrent\qmyplot.cpp:3: error: Constructor for 'QMyPlot' must explicitly initialize the base class 'QCPAxisRect' which does not have a default constructor
    

    Which is not fully clear to me. I am hoping to get some clarifications on how to prevent this error and how to properly override the methods that are defined in QCustomPlot.cpp

    J 1 Reply Last reply 4 Apr 2024, 05:54
    0
    • L lukutis222
      4 Apr 2024, 05:42

      I am working with QCustomPlot on the QT to write my application. This question is related to QCustomPlot but more related to C++ in general hence I have decided to ask here.

      QCustomPlot source files can be found here:
      https://www.qcustomplot.com/index.php/download

      I want to slightly change the funcionality of the QCustomPlot by overriding some methods (mousePressEvent and mouseMoveEvent) to achieve simillar funcionality as described here: https://www.qcustomplot.com/index.php/support/forum/1887

      So I have tried to create a custom class named QMyPlot and made it a subclass of QCPAxisRect.

      See my QMyPlot.h:

      #ifndef QMYPLOT_H
      #define QMYPLOT_H
      
      #include "qcustomplot.h"
      
      class QMyPlot : public QCPAxisRect
      {
      public:
          QMyPlot();
      
      };
      
      #endif // QMYPLOT_H
      
      

      and QMyPlot.cpp:

      #include "qmyplot.h"
      
      QMyPlot::QMyPlot()
      {
      
      }
      
      
      

      I am getting an error:

      C:\Users\my_user\Desktop\WORK\QT\uCurrent\uCurrent\qmyplot.cpp:3: error: Constructor for 'QMyPlot' must explicitly initialize the base class 'QCPAxisRect' which does not have a default constructor
      

      Which is not fully clear to me. I am hoping to get some clarifications on how to prevent this error and how to properly override the methods that are defined in QCustomPlot.cpp

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 4 Apr 2024, 05:54 last edited by
      #2

      @lukutis222 Your constructor must call a constructor of the base class:

      QMyPlot::QMyPlot()
          : QCPAxisRect(...)
      {
      
      }
      

      Since there is no QCPAxisRect default constructor (as the error says) you have to call one of the others.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply 4 Apr 2024, 06:33
      1
      • J jsulm
        4 Apr 2024, 05:54

        @lukutis222 Your constructor must call a constructor of the base class:

        QMyPlot::QMyPlot()
            : QCPAxisRect(...)
        {
        
        }
        

        Since there is no QCPAxisRect default constructor (as the error says) you have to call one of the others.

        L Offline
        L Offline
        lukutis222
        wrote on 4 Apr 2024, 06:33 last edited by lukutis222 4 Apr 2024, 06:33
        #3

        @jsulm
        Thanks. I have managed to get it to work using the following code:

        QMyPlot.h:

        #ifndef QMYPLOT_H
        #define QMYPLOT_H
        
        #include "qcustomplot.h"
        
        class QMyPlot : public QCPAxisRect
        {
        public:
            QMyPlot(QCustomPlot *parentPlot);
        
        private slots:
        
            void mousePressEvent(QMouseEvent *event, const QVariant &details);
        
        
        };
        
        #endif // QMYPLOT_H
        

        and my QMyPlot.cpp:

        #include "qmyplot.h"
        
        QMyPlot::QMyPlot(QCustomPlot *parentPlot):QCPAxisRect(parentPlot,true)
        
        {
        
        }
        
        
        
        void QMyPlot::mousePressEvent(QMouseEvent *event, const QVariant &details)
        {
            qDebug("Mouse press custom event");
        }
        

        In my MainWindow.cpp I can now create a constructor of my custom QMyPlot class:

            QMyPlot test(ui->customPlot);
        
        

        I am now trying to figure out how to get this mousePressEvent to actually work and override the existing QCPAxisRect mousePressEvent. The original method in the QCustomPlot.cpp is declared as following:

        void QCPAxisRect::mousePressEvent(QMouseEvent *event, const QVariant &details)
        {
          Q_UNUSED(details)
          if (event->buttons() & Qt::LeftButton)
          {
            mDragging = true;
            // initialize antialiasing backup in case we start dragging:
            if (mParentPlot->noAntialiasingOnDrag())
            {
              mAADragBackup = mParentPlot->antialiasedElements();
              mNotAADragBackup = mParentPlot->notAntialiasedElements();
            }
            // Mouse range dragging interaction:
            if (mParentPlot->interactions().testFlag(QCP::iRangeDrag))
            {
              mDragStartHorzRange.clear();
              foreach (QPointer<QCPAxis> axis, mRangeDragHorzAxis)
                mDragStartHorzRange.append(axis.isNull() ? QCPRange() : axis->range());
              mDragStartVertRange.clear();
              foreach (QPointer<QCPAxis> axis, mRangeDragVertAxis)
                mDragStartVertRange.append(axis.isNull() ? QCPRange() : axis->range());
            }
          }
        }
        
        J 1 Reply Last reply 4 Apr 2024, 06:51
        0
        • L lukutis222
          4 Apr 2024, 06:33

          @jsulm
          Thanks. I have managed to get it to work using the following code:

          QMyPlot.h:

          #ifndef QMYPLOT_H
          #define QMYPLOT_H
          
          #include "qcustomplot.h"
          
          class QMyPlot : public QCPAxisRect
          {
          public:
              QMyPlot(QCustomPlot *parentPlot);
          
          private slots:
          
              void mousePressEvent(QMouseEvent *event, const QVariant &details);
          
          
          };
          
          #endif // QMYPLOT_H
          

          and my QMyPlot.cpp:

          #include "qmyplot.h"
          
          QMyPlot::QMyPlot(QCustomPlot *parentPlot):QCPAxisRect(parentPlot,true)
          
          {
          
          }
          
          
          
          void QMyPlot::mousePressEvent(QMouseEvent *event, const QVariant &details)
          {
              qDebug("Mouse press custom event");
          }
          

          In my MainWindow.cpp I can now create a constructor of my custom QMyPlot class:

              QMyPlot test(ui->customPlot);
          
          

          I am now trying to figure out how to get this mousePressEvent to actually work and override the existing QCPAxisRect mousePressEvent. The original method in the QCustomPlot.cpp is declared as following:

          void QCPAxisRect::mousePressEvent(QMouseEvent *event, const QVariant &details)
          {
            Q_UNUSED(details)
            if (event->buttons() & Qt::LeftButton)
            {
              mDragging = true;
              // initialize antialiasing backup in case we start dragging:
              if (mParentPlot->noAntialiasingOnDrag())
              {
                mAADragBackup = mParentPlot->antialiasedElements();
                mNotAADragBackup = mParentPlot->notAntialiasedElements();
              }
              // Mouse range dragging interaction:
              if (mParentPlot->interactions().testFlag(QCP::iRangeDrag))
              {
                mDragStartHorzRange.clear();
                foreach (QPointer<QCPAxis> axis, mRangeDragHorzAxis)
                  mDragStartHorzRange.append(axis.isNull() ? QCPRange() : axis->range());
                mDragStartVertRange.clear();
                foreach (QPointer<QCPAxis> axis, mRangeDragVertAxis)
                  mDragStartVertRange.append(axis.isNull() ? QCPRange() : axis->range());
              }
            }
          }
          
          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 4 Apr 2024, 06:51 last edited by
          #4

          @lukutis222 said in Overriding QCustomPlot library method with your own method:

          I am now trying to figure out how to get this mousePressEvent to actually work and override the existing QCPAxisRect mousePressEvent

          You need to override mousePressEvent in your class. See https://doc.qt.io/qt-6/eventsandfilters.html

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          L 1 Reply Last reply 4 Apr 2024, 08:03
          0
          • J jsulm
            4 Apr 2024, 06:51

            @lukutis222 said in Overriding QCustomPlot library method with your own method:

            I am now trying to figure out how to get this mousePressEvent to actually work and override the existing QCPAxisRect mousePressEvent

            You need to override mousePressEvent in your class. See https://doc.qt.io/qt-6/eventsandfilters.html

            L Offline
            L Offline
            lukutis222
            wrote on 4 Apr 2024, 08:03 last edited by lukutis222 4 Apr 2024, 08:05
            #5

            @jsulm
            Thanks for suggesting that. I have read it and I have an idea how to implement this.

            For example, the original mousePressEvent for the QCPAxisRect only handles the logic when the left mouse button is pressed. I have extended the funcionality of that by adding a debug print message when the right mouse button is pressed. When the left mouse button is pressed, I call the origin method.

            void QMyPlot::mousePressEvent(QMouseEvent *event, const QVariant &details)
            {
            
                if (event->button() == Qt::RightButton)
                {
                    qDebug("Right mouse Press Event\n");
                }
                else
                {
                    qDebug("Left mouse Press Event\n");
                    QCPAxisRect::mousePressEvent(event,details);
                }
            
            }
            
            

            But I still do not get how to actually get this method to work? What do I need to do in my MainWindow.cpp after creating a class object to ensure that when I press right button it actually triggers this

            J 1 Reply Last reply 4 Apr 2024, 08:13
            0
            • L lukutis222
              4 Apr 2024, 08:03

              @jsulm
              Thanks for suggesting that. I have read it and I have an idea how to implement this.

              For example, the original mousePressEvent for the QCPAxisRect only handles the logic when the left mouse button is pressed. I have extended the funcionality of that by adding a debug print message when the right mouse button is pressed. When the left mouse button is pressed, I call the origin method.

              void QMyPlot::mousePressEvent(QMouseEvent *event, const QVariant &details)
              {
              
                  if (event->button() == Qt::RightButton)
                  {
                      qDebug("Right mouse Press Event\n");
                  }
                  else
                  {
                      qDebug("Left mouse Press Event\n");
                      QCPAxisRect::mousePressEvent(event,details);
                  }
              
              }
              
              

              But I still do not get how to actually get this method to work? What do I need to do in my MainWindow.cpp after creating a class object to ensure that when I press right button it actually triggers this

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 4 Apr 2024, 08:13 last edited by
              #6

              @lukutis222 said in Overriding QCustomPlot library method with your own method:

              What do I need to do in my MainWindow.cpp after creating a class object to ensure that when I press right button it actually triggers this

              Now you're talking about MainWindow - but you're overriding the event in QMyPlot. So where do you want to handle mouse press event?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              L 1 Reply Last reply 4 Apr 2024, 08:20
              0
              • J jsulm
                4 Apr 2024, 08:13

                @lukutis222 said in Overriding QCustomPlot library method with your own method:

                What do I need to do in my MainWindow.cpp after creating a class object to ensure that when I press right button it actually triggers this

                Now you're talking about MainWindow - but you're overriding the event in QMyPlot. So where do you want to handle mouse press event?

                L Offline
                L Offline
                lukutis222
                wrote on 4 Apr 2024, 08:20 last edited by lukutis222 4 Apr 2024, 08:22
                #7

                @jsulm

                I do not fully understand what do you mean. I simply want to override the existing library method:
                void QCPAxisRect::mousePressEvent(QMouseEvent *event, const QVariant &details)

                For that I have created a new class which is a subclass of QCPAxisRect and overridden the mousePressEvent method. Whats next? As you can see from my overridden method:

                void QMyPlot::mousePressEvent(QMouseEvent *event, const QVariant &details)
                {
                
                    if (event->button() == Qt::RightButton)
                    {
                        qDebug("Right mouse Press Event\n");
                    }
                    else
                    {
                        qDebug("Left mouse Press Event\n");
                        QCPAxisRect::mousePressEvent(event,details);
                    }
                
                }
                

                When the right mouse button is pressed, it is supposed to print debug message "Right mouse Press Event". I can launch the application and click the right mouse button but it will not print the debug message it so I assume I am missing something to actually trigger this.

                In my MainWindow.cpp I create signals:

                    connect(ui->customPlot, &QCustomPlot::mouseMove, this, &MainWindow::showPointToolTip);
                    connect(ui->customPlot, &QCustomPlot::mouseDoubleClick, this, &MainWindow::SavePoint);
                    connect(ui->customPlot, &QCustomPlot::selectionChangedByUser, this, &MainWindow::Calculate_average_selected);
                    connect(ui->customPlot, &QCustomPlot::mousePress, this, &MainWindow::ChangeScrollMode);
                

                Perhaps I need to replace this with my new class ?

                J 1 Reply Last reply 4 Apr 2024, 08:27
                0
                • L lukutis222
                  4 Apr 2024, 08:20

                  @jsulm

                  I do not fully understand what do you mean. I simply want to override the existing library method:
                  void QCPAxisRect::mousePressEvent(QMouseEvent *event, const QVariant &details)

                  For that I have created a new class which is a subclass of QCPAxisRect and overridden the mousePressEvent method. Whats next? As you can see from my overridden method:

                  void QMyPlot::mousePressEvent(QMouseEvent *event, const QVariant &details)
                  {
                  
                      if (event->button() == Qt::RightButton)
                      {
                          qDebug("Right mouse Press Event\n");
                      }
                      else
                      {
                          qDebug("Left mouse Press Event\n");
                          QCPAxisRect::mousePressEvent(event,details);
                      }
                  
                  }
                  

                  When the right mouse button is pressed, it is supposed to print debug message "Right mouse Press Event". I can launch the application and click the right mouse button but it will not print the debug message it so I assume I am missing something to actually trigger this.

                  In my MainWindow.cpp I create signals:

                      connect(ui->customPlot, &QCustomPlot::mouseMove, this, &MainWindow::showPointToolTip);
                      connect(ui->customPlot, &QCustomPlot::mouseDoubleClick, this, &MainWindow::SavePoint);
                      connect(ui->customPlot, &QCustomPlot::selectionChangedByUser, this, &MainWindow::Calculate_average_selected);
                      connect(ui->customPlot, &QCustomPlot::mousePress, this, &MainWindow::ChangeScrollMode);
                  

                  Perhaps I need to replace this with my new class ?

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 4 Apr 2024, 08:27 last edited by
                  #8

                  @lukutis222 said in Overriding QCustomPlot library method with your own method:

                  I can launch the application and click the right mouse button but it will not print the debug message it so I assume I am missing something to actually trigger this

                  Where in the application do you press the mouse? Since you're overriding mousePressEvent in QMyPlot you need to press mouse when the cursor is over QMyPlot.

                  Also this is completely wrong (please read again the link I gave you):

                  private slots:
                  
                      void mousePressEvent(QMouseEvent *event, const QVariant &details);
                  

                  mousePressEvent is NOT a slot!
                  And as a tip: add override keyword if you're overriding an inherited method:

                  void mousePressEvent(QMouseEvent *event, const QVariant &details) override;
                  

                  In this case compiler will tell you that you're not overriding properly if you do something wrong.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  L 1 Reply Last reply 4 Apr 2024, 08:51
                  1
                  • J jsulm
                    4 Apr 2024, 08:27

                    @lukutis222 said in Overriding QCustomPlot library method with your own method:

                    I can launch the application and click the right mouse button but it will not print the debug message it so I assume I am missing something to actually trigger this

                    Where in the application do you press the mouse? Since you're overriding mousePressEvent in QMyPlot you need to press mouse when the cursor is over QMyPlot.

                    Also this is completely wrong (please read again the link I gave you):

                    private slots:
                    
                        void mousePressEvent(QMouseEvent *event, const QVariant &details);
                    

                    mousePressEvent is NOT a slot!
                    And as a tip: add override keyword if you're overriding an inherited method:

                    void mousePressEvent(QMouseEvent *event, const QVariant &details) override;
                    

                    In this case compiler will tell you that you're not overriding properly if you do something wrong.

                    L Offline
                    L Offline
                    lukutis222
                    wrote on 4 Apr 2024, 08:51 last edited by lukutis222 4 Apr 2024, 08:52
                    #9

                    @jsulm
                    Thanks for the tip. I have fixed my declaration as you have suggested and added override keyword.

                    I am slowly starting to grasp how my QMyPlot supposed to work. See my MainWindow.cpp:

                    
                    MainWindow::MainWindow(QWidget *parent)
                        : QMainWindow(parent), ui(new Ui::MainWindow)
                    {
                        sample_counter = 0;
                        sample_counter_1_sec = 0;
                    
                        ui->setupUi(this);
                    
                        setup_graph(ui->customPlot);
                        connect(ui->customPlot, &QCustomPlot::mouseMove, this, &MainWindow::showPointToolTip);
                        connect(ui->customPlot, &QCustomPlot::mouseDoubleClick, this, &MainWindow::SavePoint);
                        connect(ui->customPlot, &QCustomPlot::selectionChangedByUser, this, &MainWindow::Calculate_average_selected);
                        connect(ui->customPlot, &QCustomPlot::mousePress, this, &MainWindow::ChangeScrollMode);
                    
                    
                    }
                    
                    
                    
                    void MainWindow::setup_graph(QCustomPlot *customPlot)
                    {
                    
                        current_upper_limit = 10;
                        current_lower_limit = 0;
                    
                        voltage_upper_limit = 5;
                        voltage_lower_limit = 0;
                    
                        QColor accentColor = QColor(Qt::red);
                        QColor themeColor = QColor(Qt::white);
                        QColor themeBackgroundColor = QColor(53, 57, 53);
                        QColor traceColor = accentColor;
                    
                        QMyPlot test(customPlot);
                    
                    
                        foreach (QCPAxisRect *rect, customPlot->axisRects())
                        {
                            foreach (QCPAxis *axis, rect->axes())
                            {
                                axis->setLabelColor(themeColor);
                                axis->setBasePen(QPen(themeColor, 1));
                                axis->setTickPen(QPen(themeColor, 1));
                                axis->setSubTickPen(QPen(themeColor, 1));
                                axis->setTickLabelColor(themeColor);
                                axis->grid()->setPen(QPen(themeColor, 0.5, Qt::DotLine));
                                axis->grid()->setSubGridVisible(false);
                    
                                axis->setSelectedTickLabelColor(accentColor);
                                axis->setSelectedLabelColor(accentColor);
                                axis->setSelectedBasePen(QPen(accentColor, 1));
                                axis->setSelectedSubTickPen(QPen(accentColor, 1));
                                axis->setSelectedTickPen(QPen(accentColor, 1));
                            }
                        }
                        ui->customPlot->setBackground(QBrush(themeBackgroundColor));
                    
                        // SETUP GRAPH 0 for Current
                        customPlot->addGraph();
                        customPlot->graph(0)->setScatterStyle(QCPScatterStyle::ssCircle);
                        customPlot->graph(0)->setLineStyle(QCPGraph::lsLine);
                        QPen pen_current;
                        pen_current.setWidth(2);
                        pen_current.setColor(QColor(80, 200, 120));
                        customPlot->graph(0)->setPen(pen_current);
                    
                    
                    
                        // SETUP GRAPH 1 for Voltage
                        // customPlot->addGraph();
                        customPlot->addGraph(customPlot->xAxis, customPlot->yAxis2);
                        customPlot->graph(1)->setScatterStyle(QCPScatterStyle::ssCross);
                        customPlot->graph(1)->setLineStyle(QCPGraph::lsLine);
                        QPen pen_voltage;
                        pen_voltage.setWidth(2);
                        pen_voltage.setColor(QColor(225,225,50));
                        customPlot->graph(1)->setPen(pen_voltage);
                    
                    
                        // SETUP GRAPH 2 for cursors
                        customPlot->addGraph(customPlot->xAxis, customPlot->yAxis);
                        customPlot->graph(2)->setLineStyle((QCPGraph::LineStyle)QCPGraph::lsNone);
                        customPlot->graph(2)->setScatterStyle(QCPScatterStyle::ssTriangle);
                        QPen pen_cursor;
                        pen_cursor.setWidth(20);
                        pen_cursor.setColor(QColor(244, 200, 0));
                        customPlot->graph(2)->setPen(pen_cursor);
                    
                    
                    
                    
                        customPlot->xAxis->setLabel("Time(h:m:s)");
                    
                    
                        customPlot->yAxis->setLabel("Current (mA)");
                        customPlot->yAxis->setRange(current_lower_limit, current_upper_limit);
                        customPlot->yAxis2->setVisible(true);
                        customPlot->yAxis2->setLabel("Voltage (V)");
                        customPlot->yAxis2->setRange(voltage_lower_limit, voltage_upper_limit);
                    
                        connect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), this, SLOT(xAxisChanged(QCPRange)));
                    
                        customPlot->legend->setVisible(true);
                        customPlot->graph(0)->setName("Current");
                        customPlot->graph(1)->setName("Voltage");
                        customPlot->graph(2)->setName("Cursor");
                    
                        customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables | QCP::iMultiSelect);
                        customPlot->setSelectionRectMode(QCP::srmSelect);
                    
                        customPlot->graph(0)->setSelectable(QCP::stDataRange);
                        customPlot->graph(1)->setSelectable(QCP::stDataRange);
                        customPlot->graph(2)->setSelectable(QCP::stSingleData);
                    
                        foreach (QCPAxisRect *rect, customPlot->axisRects())
                        {
                            rect->setRangeDrag(Qt::Horizontal);
                            rect->setRangeZoom(Qt::Horizontal);
                        }
                    
                    
                    }
                    

                    My main QCustomPlot widget is as shown here:
                    4d0a7b88-b760-432c-b9ee-3d963efd3a07-image.png

                    My setup_graph method contains all configuration and initialization of my QCustomPlot. I think somewhere inside here I must utilize my new class QMyPlot instead of using customPlot .

                    I am currently reading more about QAxisRect to fully figure out how and where should I use my new class QMyPlot object.

                    J 1 Reply Last reply 4 Apr 2024, 08:54
                    0
                    • L lukutis222
                      4 Apr 2024, 08:51

                      @jsulm
                      Thanks for the tip. I have fixed my declaration as you have suggested and added override keyword.

                      I am slowly starting to grasp how my QMyPlot supposed to work. See my MainWindow.cpp:

                      
                      MainWindow::MainWindow(QWidget *parent)
                          : QMainWindow(parent), ui(new Ui::MainWindow)
                      {
                          sample_counter = 0;
                          sample_counter_1_sec = 0;
                      
                          ui->setupUi(this);
                      
                          setup_graph(ui->customPlot);
                          connect(ui->customPlot, &QCustomPlot::mouseMove, this, &MainWindow::showPointToolTip);
                          connect(ui->customPlot, &QCustomPlot::mouseDoubleClick, this, &MainWindow::SavePoint);
                          connect(ui->customPlot, &QCustomPlot::selectionChangedByUser, this, &MainWindow::Calculate_average_selected);
                          connect(ui->customPlot, &QCustomPlot::mousePress, this, &MainWindow::ChangeScrollMode);
                      
                      
                      }
                      
                      
                      
                      void MainWindow::setup_graph(QCustomPlot *customPlot)
                      {
                      
                          current_upper_limit = 10;
                          current_lower_limit = 0;
                      
                          voltage_upper_limit = 5;
                          voltage_lower_limit = 0;
                      
                          QColor accentColor = QColor(Qt::red);
                          QColor themeColor = QColor(Qt::white);
                          QColor themeBackgroundColor = QColor(53, 57, 53);
                          QColor traceColor = accentColor;
                      
                          QMyPlot test(customPlot);
                      
                      
                          foreach (QCPAxisRect *rect, customPlot->axisRects())
                          {
                              foreach (QCPAxis *axis, rect->axes())
                              {
                                  axis->setLabelColor(themeColor);
                                  axis->setBasePen(QPen(themeColor, 1));
                                  axis->setTickPen(QPen(themeColor, 1));
                                  axis->setSubTickPen(QPen(themeColor, 1));
                                  axis->setTickLabelColor(themeColor);
                                  axis->grid()->setPen(QPen(themeColor, 0.5, Qt::DotLine));
                                  axis->grid()->setSubGridVisible(false);
                      
                                  axis->setSelectedTickLabelColor(accentColor);
                                  axis->setSelectedLabelColor(accentColor);
                                  axis->setSelectedBasePen(QPen(accentColor, 1));
                                  axis->setSelectedSubTickPen(QPen(accentColor, 1));
                                  axis->setSelectedTickPen(QPen(accentColor, 1));
                              }
                          }
                          ui->customPlot->setBackground(QBrush(themeBackgroundColor));
                      
                          // SETUP GRAPH 0 for Current
                          customPlot->addGraph();
                          customPlot->graph(0)->setScatterStyle(QCPScatterStyle::ssCircle);
                          customPlot->graph(0)->setLineStyle(QCPGraph::lsLine);
                          QPen pen_current;
                          pen_current.setWidth(2);
                          pen_current.setColor(QColor(80, 200, 120));
                          customPlot->graph(0)->setPen(pen_current);
                      
                      
                      
                          // SETUP GRAPH 1 for Voltage
                          // customPlot->addGraph();
                          customPlot->addGraph(customPlot->xAxis, customPlot->yAxis2);
                          customPlot->graph(1)->setScatterStyle(QCPScatterStyle::ssCross);
                          customPlot->graph(1)->setLineStyle(QCPGraph::lsLine);
                          QPen pen_voltage;
                          pen_voltage.setWidth(2);
                          pen_voltage.setColor(QColor(225,225,50));
                          customPlot->graph(1)->setPen(pen_voltage);
                      
                      
                          // SETUP GRAPH 2 for cursors
                          customPlot->addGraph(customPlot->xAxis, customPlot->yAxis);
                          customPlot->graph(2)->setLineStyle((QCPGraph::LineStyle)QCPGraph::lsNone);
                          customPlot->graph(2)->setScatterStyle(QCPScatterStyle::ssTriangle);
                          QPen pen_cursor;
                          pen_cursor.setWidth(20);
                          pen_cursor.setColor(QColor(244, 200, 0));
                          customPlot->graph(2)->setPen(pen_cursor);
                      
                      
                      
                      
                          customPlot->xAxis->setLabel("Time(h:m:s)");
                      
                      
                          customPlot->yAxis->setLabel("Current (mA)");
                          customPlot->yAxis->setRange(current_lower_limit, current_upper_limit);
                          customPlot->yAxis2->setVisible(true);
                          customPlot->yAxis2->setLabel("Voltage (V)");
                          customPlot->yAxis2->setRange(voltage_lower_limit, voltage_upper_limit);
                      
                          connect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), this, SLOT(xAxisChanged(QCPRange)));
                      
                          customPlot->legend->setVisible(true);
                          customPlot->graph(0)->setName("Current");
                          customPlot->graph(1)->setName("Voltage");
                          customPlot->graph(2)->setName("Cursor");
                      
                          customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables | QCP::iMultiSelect);
                          customPlot->setSelectionRectMode(QCP::srmSelect);
                      
                          customPlot->graph(0)->setSelectable(QCP::stDataRange);
                          customPlot->graph(1)->setSelectable(QCP::stDataRange);
                          customPlot->graph(2)->setSelectable(QCP::stSingleData);
                      
                          foreach (QCPAxisRect *rect, customPlot->axisRects())
                          {
                              rect->setRangeDrag(Qt::Horizontal);
                              rect->setRangeZoom(Qt::Horizontal);
                          }
                      
                      
                      }
                      

                      My main QCustomPlot widget is as shown here:
                      4d0a7b88-b760-432c-b9ee-3d963efd3a07-image.png

                      My setup_graph method contains all configuration and initialization of my QCustomPlot. I think somewhere inside here I must utilize my new class QMyPlot instead of using customPlot .

                      I am currently reading more about QAxisRect to fully figure out how and where should I use my new class QMyPlot object.

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 4 Apr 2024, 08:54 last edited by
                      #10

                      @lukutis222 said in Overriding QCustomPlot library method with your own method:

                      I am currently reading more about QAxisRect to fully figure out how and where should I use my new class QMyPlot object.

                      You can add QMyPlot instance manually in code instead of in designer.
                      But you also can do that in designer: https://doc.qt.io/qt-6/designer-using-custom-widgets.html

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      L 1 Reply Last reply 4 Apr 2024, 09:06
                      0
                      • J jsulm
                        4 Apr 2024, 08:54

                        @lukutis222 said in Overriding QCustomPlot library method with your own method:

                        I am currently reading more about QAxisRect to fully figure out how and where should I use my new class QMyPlot object.

                        You can add QMyPlot instance manually in code instead of in designer.
                        But you also can do that in designer: https://doc.qt.io/qt-6/designer-using-custom-widgets.html

                        L Offline
                        L Offline
                        lukutis222
                        wrote on 4 Apr 2024, 09:06 last edited by
                        #11

                        @jsulm
                        But that is not going to do anything on its own. QAxisRect is nothing on its own without the QCustomPlot

                        J 1 Reply Last reply 4 Apr 2024, 09:09
                        0
                        • L lukutis222
                          4 Apr 2024, 09:06

                          @jsulm
                          But that is not going to do anything on its own. QAxisRect is nothing on its own without the QCustomPlot

                          J Offline
                          J Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 4 Apr 2024, 09:09 last edited by
                          #12

                          @lukutis222 Then add them in code

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0

                          1/12

                          4 Apr 2024, 05:42

                          • Login

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