Getting a pointer out of on_pushButton_clicked()
-
I'm having a problem with getting inputs out of on_pushButton_clicked().
My program reads a file and gets the data into a structure, which is created inside the function on_pushButton_clicked().
Then I want to get that structure out of on_pushButton_clicked() to use it in future functions. How can I do it? (I want to get the "topo" pointer out, without needing to call the function on_pushButton_clicked() )
Here is my code:
Thank you in advance.Mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QFileDialog>
#include "Estrutura.h"
#include <QString>
#include <QTextStream>
#include <QMessageBox>MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_pushButton_clicked()
{
QString linha;
QStringList list;
float tempo,velocidade,pack1,pack2,pack3,cellT;
header topo;QFile file(QFileDialog::getOpenFileName(this,tr("Open File"),"",tr("Text Files (*.txt)"))); if(!file.open(QIODevice::ReadOnly)) QMessageBox::information(0,"info",file.errorString()); topo=inicializalista(); QTextStream in(&file);
while(!in.atEnd())
{linha=in.readLine(); list=linha.split(" "); tempo=list[0].toFloat(); velocidade=list[1].toFloat(); pack1=list[2].toFloat(); pack2=list[3].toFloat(); pack3=list[4].toFloat(); cellT=list[5].toFloat(); topo=inserelista(topo,tempo,velocidade,pack1,pack2,pack3,cellT);
}
imprimelista(topo);}
lista.cpp:
#include "Estrutura.h"
#include <stdlib.h>
#include <QDebug>header inicializalista(){
header novo; novo=(header)calloc(1,sizeof(dados));//alocação de memória novo->cellT=0; //inicialização de todas as variáveis novo->pack1=0; novo->pack2=0; novo->pack3=0; novo->tempo=0; novo->velocidade=0; novo->next=nullptr; return novo;
}
header inserelista(header topo,float tempo,float velocidade,float pack1,float pack2,float pack3,float cellT){
if( (topo->cellT==0.0) && (topo->pack1==0.0) && (topo->pack2==0.0) && (topo->pack3==0.0) && (topo->velocidade==0.0) && (topo->tempo==0.0)) { topo->cellT=cellT; //caso a lista esteja vazia faz-se a substituição de valores topo->pack1=pack1; topo->pack2=pack2; topo->pack3=pack3; topo->tempo=tempo; topo->velocidade=velocidade; } else { header novo; //inserção de um novo bloco no fim da lista header aux; novo=(header)calloc(1,sizeof(dados)); for(aux=topo;aux->next!=nullptr;aux=aux->next); aux->next=novo; novo->next=nullptr; novo->cellT=cellT; novo->pack1=pack1; novo->pack2=pack2; novo->pack3=pack3; novo->tempo=tempo; novo->velocidade=velocidade; } return topo;
}
void imprimelista(header topo)
{
header aux;for(aux=topo;aux!=nullptr;aux=aux->next) //impressão da lista na consola para efeitos de verificação { qDebug()<< "tempo" << aux->tempo; qDebug()<< "velocidade" << aux->velocidade; qDebug()<< "pack1" << aux->pack1; qDebug()<< "pack2" << aux->pack2 ; qDebug()<< "pack3" << aux->pack3; qDebug()<< "cellT" << aux->cellT<<"\n"; }
}
Estrutura.h:
#ifndef ESTRUTURA_H
#define ESTRUTURA_Hclass dados{
public:
float tempo;
float velocidade;
float pack1;
float pack2;
float pack3;
float cellT;dados* next;
};
typedef dados* header;
header inicializalista();
header inserelista(header topo,float tempo,float velocidade,float pack1,float pack2,float pack3,float cellT);
void imprimelista(header topo);#endif // ESTRUTURA_H
-
@reis2211 said in Getting a pointer out of on_pushButton_clicked():
How can I do it?
Make topo member variable of your class...
-
@jsulm said in Getting a pointer out of on_pushButton_clicked():
member variable of your class.
Could you explain, more precisely, how that would help me getting "topo" out of on_pushButton_clicked, i'm a bit confused...
Thank you, again, in advance. -
@reis2211 If topo is member variable it can be accessed everywhere in that class like any other member variable.
class MainWindow... private: header topo; // Can be accessed everywhere in MainWindow
Or do you want to access it outside of MainWindow?
In that case you cann add a getter method:class MainWindow... public: header getTopo() { return topo; } private: header topo; // Can be accessed everywhere in MainWindow