PDA

View Full Version : How to get a spinbox value to a variable ?



Flavio Mesquita
3rd June 2018, 18:53
Hi everyone,
I´m new to programming and Qt, but i loved the interface and I´m trying to learn and improve my skills.
I received a task to create a simple interface that is composed of 4 spinboxes, 2 radio buttons, 2 checboxes and a command link button(RUN).
I have to select the values on each one and when I press the RUN button it has to get all those values chosen and create a txt file, where each line is one of the values, this file will be later read by another program as a configuration file ini.
I created variables that should receive these values and by the end are read by another function that creates the txt file.
My problem is how to assign the values changed on the spinboxes and assign it to the variables. I can get the default values, when the interface initializes assigned to the variables, but when I select the values I want and click RUN nothing happens. I´m pretty sure it is something very simple, I just don’t know what. Can u give some idea what am I doing wrong?
Thanks in advance.

header code:

#ifndef INTERFACE2_H
#define INTERFACE2_H
#include <QtGui>
#include <QWidget>

namespace Ui {
class interface2;
}

class interface2 : public QWidget
{
Q_OBJECT

public:
explicit interface2(QWidget *parent = 0);
~interface2();

private:
Ui::interface2 *ui;

qint64 number_traces;
qint64 trace_samples;
qreal sampling_rate;
qint64 wavelet_freq;
qint64 polarity;
qint64 noise;
qint64 standard_reflec;

void write();


private slots:
void on_RUN_clicked();
};

#endif // INTERFACE2_H

cpp code

#include "interface2.h"
#include "ui_interface2.h"
#include <QtCore/QString>
#include <QtCore/QFile>
#include <QtCore/QTextStream>
#include <QMessageBox>

interface2::interface2(QWidget *parent) :
QWidget(parent),
ui(new Ui::interface2)

{

ui->setupUi(this);
on_RUN_clicked();
write();

}

interface2::~interface2()
{
delete ui;
}

void interface2::on_RUN_clicked()
{

number_traces = ui->traces->value();
trace_samples = ui->samples->value();
sampling_rate = ui->rate->value();
wavelet_freq = ui->freq->value();

{
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;
}
}
}


void interface2::write()
{

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"));
//return 1;
}

/* Point a QTextStream object at the file */
QTextStream outStream(&outputFile);

/* Write the line to the file */
outStream << "////////////////////////////////////////////////////////////////////////////////////"<<endl
<< "// INITIALIZATION FILE FOR SISMICA1 PROGRAM //"<<endl
<< "// //"<<endl
<< "// This is the initialization file, it is not required for running the program //"<<endl
<< "// but any change on the parameters MUST be done trough it, otherwise pre-loaded //"<<endl
<< "// values will be used. //" <<endl
<< "// //"<<endl
<< "// //"<<endl
<< "// It MUST be on the same directory as sismica1.exe //"<<endl
<< "// //"<<endl
<< "// The file generated will be named: traços.txt, it will contain the convolution //"<<endl
<< "// results. //"<<endl
<< "// //"<<endl
<< "// //"<<endl
<< "// IMPORTANT : //"<<endl
<< "// ========== //"<<endl
<< "// //"<<endl
<< "// Do not change the amount of lines in this file, except, if you include new para//"<<endl
<< "// meters on the source code; //"<<endl
<< "// DO NOT change the parameters order; //"<<endl
<< "// Use dot(.) as decimal separator; //"<<endl
<< "// There MUST NOT be any blank spaces or any non printable charactere. //"<<endl
<< "// //"<<endl
<< "// //"<<endl
<< "// The parameters are separated by a new line character, so everything along the //"<<endl
<< "// line below this header, will be considered as parameter. //"<<endl
<< "// //"<<endl
<< "// //"<<endl
<< "// Parameters //"<<endl
<< "// ========== //"<<endl
<< "// //"<<endl
<< "// Impedance file name //"<<endl
<< "// Number of traces (0 use all the traces on file) //"<<endl
<< "// Trace samples amount //"<<endl
<< "// Sampling rate //"<<endl
<< "// Wavelet peak frequency //"<<endl
<< "// Polarity //"<<endl
<< "// Flag indicating the use of noise //"<<endl
<< "// Flag indicating the use of standard reflectivity model //"<<endl
<< "// //"<<endl
<< "// Credits: Carlos da S. Claudino /Flavio Mesquita //"<<endl
<< "///////////////////////////////////////////////////////////////////////////////////"<<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();
}

Lesiok
4th June 2018, 06:53
What does it mean "nothing happens" ?

Flavio Mesquita
4th June 2018, 13:12
Hi,
i meant when i open the GUI it creates a .ini file automatically with the default values, then when i choose the values i want and then click RUN, it doesn`t update the values written to the ini file.
Anyway i solved it already by eliminating the function for create the file and putting it inside the RUN function. So, when i click the button it gets the values from the interface assign it to the variables and then updates the ini file.

Lesiok
4th June 2018, 13:49
That was the problem. Write must be called at the end of the on_RUN_clicked() and not in the constructor.