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. QtCharts doesnt show graph.
Forum Updated to NodeBB v4.3 + New Features

QtCharts doesnt show graph.

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 966 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.
  • F Offline
    F Offline
    Flavio Mesquita
    wrote on last edited by VRonin
    #1

    Hi everyone

    could i have ur attention for a few minutes, please? I´m having a problem to plot a Ricker wavelet . I did an interface where i could choose the arguments for the ricker function, save it on a .ini file and plot the ricker function. I have it running , but it doesn´t show the graph, everything compiles, it runs, the interface dialog shows up, I can select the values, save it to the ini file, but the plot window shows up very quick and disappears, so I put a sleep command at the end of it, with 10s, so I could then see the plot window, but it is blank.
    I did include charts on the .pro file
    I don’t´see anything wrong with the code, why it is not plotting? Some function that is not taking the arguments? Something wrong on my logic? If ricker function and ricker series are inside the interface, when I call the Run_clicked button the arguments should be passed to plot function.
    Any help would be much apreciated.

    here is the code:

    interface2.h

    #ifndef INTERFACE2_H
    #define INTERFACE2_H
    #include <QtGui>
    #include <QWidget>
    #include <QtCore/QString>
    #include <QtCore/QFile>
    #include <QtCore/QTextStream>
    #include <QMessageBox>
    #include <cmath>
    #include <QtCharts/QLineSeries>
    #include <QtCharts/QChartView>
    #include <unistd.h>
    
    using namespace QtCharts;
    
    namespace Ui {
    class interface2;
    }
    
    
    class interface2 : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit interface2(QWidget *parent = 0);
        ~interface2();
    
       QVector<QPointF> ricker(double f, double dt ,double length);
       QLineSeries* rickerSeries(double f);
    
        double length;
        int number_traces;
        int trace_samples;
        double sampling_rate;
        int polarity;
        double wavelet_freq;
        bool noise;
        bool standard_reflec;
        double f;
        double dt1;
        double dt;
        const double pi = 3.14159265358979323846;
    
    private:
     Ui::interface2 *ui;
     void plot();
    
    
    private slots:
        void on_RUN_clicked();
    
    };
    
    #endif // INTERFACE2_H
    

    interface2.cpp

    #include "interface2.h"
    
    #include "ui_interface2.h"
    
    
    
    
    
    const double pi = 3.14159265358979323846;
    
    
    
    
    
    QVector<QPointF> interface2::ricker(double f, double dt ,double length)
    
     {
    
       size_t N = (length - dt/2.0)/dt;
    
       QVector<QPointF> w(N);
    
       for (size_t i = 0; i < N; ++i)
    
       {
    
          double t = -length/2 + i*dt;
    
          w[i].setX(t);
    
          w[i].setY(polarity*((1.0 - 2*pi*pi*f*f*t*t) * exp(-pi*pi*f*f*t*t)));
    
       }
    
       return w;
    
     }
    
    
    
    
    
    QLineSeries* interface2::rickerSeries(double f) {
    
       auto *series = new QLineSeries;
    
       series->setName(QStringLiteral("Ricker Wavelet for f=%1").arg(f, 2));
    
       series->replace(interface2::ricker(f, dt, length));
    
       return series;
    
    
    
    }
    
    
    
    void interface2::plot()
    
    {
    
        QChartView view;
    
           view.chart()->addSeries(rickerSeries(f));
    
           view.chart()->createDefaultAxes();
    
           view.setMinimumSize(800, 600);
    
           view.show();
    
           sleep(10);
    
    
    
    }
    
    interface2::interface2(QWidget *parent) :
    
        QWidget(parent),
    
        ui(new Ui::interface2)
    
    
    
    {
    
    
    
        ui->setupUi(this);
    
    
    
    }
    
    
    
    
    
    interface2::~interface2()
    
    {
    
        delete ui;
    
    }
    
    
    
    
    
    void interface2::on_RUN_clicked()
    
    {
    
    
    
        length=ui->length->value();
    
        number_traces = ui->traces->value();
    
        trace_samples = ui->samples->value();
    
        sampling_rate = ui->rate->value();
    
        dt1=ui->rate->value();
    
        wavelet_freq = ui->freq->value();
    
        f=ui->freq->value();
    
        dt=dt1/1000;
    
    
    
        {
    
            if(ui->reflectivity->isChecked())/*refletividade padrao*/
    
            {
    
             standard_reflec= 1;
    
            }
    
            else
    
            {
    
             standard_reflec= 0;
    
            }
    
        }
    
    
    
    
    
        {
    
            if(ui->Noise->isChecked())/*introduzir ruido*/
    
            {
    
              noise= 1;
    
            }
    
            else
    
            {
    
             noise= 0;
    
            }
    
        }
    
    
    
        {
    
            if(ui->negative->isChecked())/*polaridade negativa*/
    
            {
    
              polarity=-1;
    
            }
    
    
    
        }
    
    
    
        {
    
            if(ui->positive->isChecked())/*polaridade positiva*/
    
            {
    
              polarity=1;
    
            }
    
        }
    
    
    
    
    
      {
    
    
    
        QString outputFilename = "sismica1.ini";
    
        QFile outputFile(outputFilename);
    
        outputFile.open(QIODevice::WriteOnly|QIODevice::Text);
    
    
    
        /* Check it opened OK */
    
        if(!outputFile.isOpen())
    
        {
    
            QMessageBox::warning(this,tr("Sismica"),tr("- Error, unable to open sismica1.ini for output"));
    
    
    
        }
    
    
    
        /* Point a QTextStream object at the file */
    
        QTextStream outStream(&outputFile);
    
    
    
        /* Write the line to the file */
    
        outStream << "////////////////////////////////////////////////////////////////////////////////////"<<endl
    
                  << "//                  INITIALIZATION FILE FOR SISMICA1 PROGRAM                      //"<<endl
    
    
                  << "Impedancia.txt"<<"\n"
    
                  << number_traces<<"\n"
    
                  << trace_samples<<"\n"
    
                  << sampling_rate/1000<<"\n"
    
                  << wavelet_freq<<"\n"
    
                  << polarity<<"\n"
    
                  << noise<<"\n"
    
                  << standard_reflec<<"\n";
    
    
    
        /* Close the file */
    
        outputFile.close();
    
    
    
     }
    
       plot();
    
    }
    

    main.cpp

    #include "interface2.h"
    
    #include "ui_interface2.h"
    
    #include <QApplication>
    
    
    
    
    
    int main(int argc, char *argv[])
    
    {
    
    
    
        QApplication a(argc, argv);
    
        interface2 w;
    
        w.show();
    
    
    
        return a.exec();
    
    
    
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You create a local variable in the plot method that will be destroyed at the end of the said method. Your sleep call luckily allows the widget to get shown but nothing will be painted because it's blocking the event loop.

      Either make the plot a member variable or instantiate in on the heap and make it auto-delete on close.

      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
      • F Offline
        F Offline
        Flavio Mesquita
        wrote on last edited by
        #3

        @SGaist , Thanks U r right again. Problem solved.

        1 Reply Last reply
        0

        • Login

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