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

Function not calling other than Main function



  • When I can mainPlot(); into main function
    as follows it works correctly for one time display.

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
       makePlot();
    }
    
    

    When I call makePlot(); to under another function it don't display plot.

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
       // makePlot();
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
     void MainWindow::makePlot()
     {
    
         // prepare data:
         QVector<double> x1(200), y1(200);
         for (int i=0; i<x1.size(); ++i)
         {
           x1[i] = i/(double)(x1.size()-1)*15;
           y1[i] = qCos(x1[i]*0.8+qSin(x1[i]*0.16+1.0))*qSin(x1[i]*0.25)+1;
         }
    
         // create and configure plottables:
         QCPGraph *graph1 = ui->customPlot->addGraph();
         graph1->setData(x1, y1);
         graph1->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, QPen(Qt::black, 1.5), QBrush(Qt::white), 9));
            graph1->setPen(QPen(QColor(220, 220, 0), 2));
    
         // 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);
    
         ui->customPlot->rescaleAxes();
         ui->customPlot->yAxis->setRange(0, 2); }
    
    void MainWindow::on_start_clicked()
    {
      makePlot();
    }
    
    

  • Qt Champions 2019

    @Mijaz said in Function not calling other than Main function:

    When I call makePlot(); to under another function it don't display plot

    What does this mean? Can you please explain better?
    Which part is not working? on_start_clicked()?

    "When I can mainPlot(); into main function" - what does this mean? What is mainPlot()?


  • Lifetime Qt Champion

    Hi
    It should not matter if you call if from constructor or
    buttons clicked.
    Did you check your slot is actually called?
    (on_start_clicked)¨



  • @jsulm
    When I call " makePlot();" to main function it works correctly.
    But when I call it through on_start_clicked() function it does not work.



  • @mrjj
    It works when I call it to main function.
    as follows:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        makePlot();
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
     void MainWindow::makePlot()
     {
         // prepare data:
         QVector<double> x1(2000), y1(2000);
         for (int i=0; i<x1.size(); ++i)
         {
           x1[i] = i/(double)(x1.size()-1)*100;
           y1[i] = qCos(x1[i]*0.5+qSin(x1[i]*0.16+1.0))*qSin(x1[i]*0.25)+1;
         }
         // create and configure plottables:
         QCPGraph *graph1 = ui->customPlot->addGraph();
         graph1->setData(x1, y1);
         graph1->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, QPen(Qt::black, 1.5), QBrush(Qt::white), 9));
            graph1->setPen(QPen(QColor(220, 220, 0), 2));
         // 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);
    
         ui->customPlot->rescaleAxes();
         ui->customPlot->yAxis->setRange(0, 3); }
    
     void MainWindow::on_start_clicked()
    {
    //ui->lineEdit->setText("Started ploting");
    //makePlot();
     }
    

    Screenshot from 2020-04-09 12-49-09.png

    It is not woking when I call though start button.
    as follows:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
       // makePlot();
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
     void MainWindow::makePlot()
     {
         // prepare data:
         QVector<double> x1(2000), y1(2000);
         for (int i=0; i<x1.size(); ++i)
         {
           x1[i] = i/(double)(x1.size()-1)*100;
           y1[i] = qCos(x1[i]*0.5+qSin(x1[i]*0.16+1.0))*qSin(x1[i]*0.25)+1;
         }
         // create and configure plottables:
         QCPGraph *graph1 = ui->customPlot->addGraph();
         graph1->setData(x1, y1);
         graph1->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, QPen(Qt::black, 1.5), QBrush(Qt::white), 9));
            graph1->setPen(QPen(QColor(220, 220, 0), 2));
         // 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);
    
         ui->customPlot->rescaleAxes();
         ui->customPlot->yAxis->setRange(0, 3); }
    
     void MainWindow::on_start_clicked()
    {
    ui->lineEdit->setText("Started ploting");
    makePlot();
     }
    

    Screenshot from 2020-04-09 12-50-04.png


  • Lifetime Qt Champion

    Hi
    Yes, that is indeed strange.
    I tried it here and got the same result.
    Not sure what is going on as it should work the same so i suspect
    QCustomPlot likes some of it to be setup before shown
    as that the only real difference running in the constructor and then in a button's click.
    i will have a look tonight when better time :)



  • The only difference is UI Refresh, MainWindow constructor is called BEFORE UI is displayed so customplot redraws everything when MainWindow is first displayed. On the other hand, when you call makePlot() in the button slot, then customplot is not being refreshed.

    Looking at the documentation, looks like you have to call replot() method at the end - to draw changes.



  • @mrjj
    yes it seems simple but not works.
    I am trying myself. Please share with me if you resoulve this issue.


  • Lifetime Qt Champion

    Hi
    Your code works for me with

    void MainWindow::on_pushButton_released()
    {
         makePlot();
         ui->customPlot->replot();
    }
    

    as @Taytoo mentions.
    alt text


Log in to reply