PDA

View Full Version : QtCharts doesnt show lineseries



Flavio Mesquita
12th June 2018, 20:27
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::Te xt);



/* 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();



}

d_stranz
12th June 2018, 20:46
Please use CODE tags when posting source code. Geez. How many times do people have to say that?



void interface2:lot()
{
QChartView view;

view.chart()->addSeries(rickerSeries(f));

view.chart()->createDefaultAxes();

view.setMinimumSize(800, 600);

view.show();

sleep(10);

}

Do you understand object lifetimes in C++? What do you think happens to the local QChartView instance you have created on the stack when this slot exits? As for the series not being displayed, the chart will not update itself until the Qt event loop has a chance to run, and that won't happen until the slot exits. And when the slot exits, what happens to the QChartView instance?

It seems to me that you need to take a few steps back and study some of the Qt examples (and C++) until you understand what event-driven programming is all about. You can't take a functional logic C or C++ program and simply convert the functional logic flow and expect it to work the same way in an event-driven framework. In an event-driven program, your code is not in charge of the flow of execution. The Qt event loop is, and it will call functions in your code that are tied to user interface or system events, not the other way around.