How Can i get array in another class
-
@anh_ph the funcToGetData(); can be a slot wich get data from GUI elements ?
And if classA isn't child od classB . How can i call it
@dziko147 said in How Can i get array in another class:
@anh_ph the funcToGetData(); can be a slot wich get data from GUI elements ?
And if classA isn't child od classB . How can i call it
If you want to get data from classA, you must get an exact object of it, which works with the data. If you can't do it, then try to apply singleton to classA.
Btw, you can't get an array in another class, but you can get an array in an object of another class. -
to resume what should i do to set data from GUI element in an array then get this array in another class .
Btw I tried many solution but usually I get an empty array in the other class :)
@dziko147 said in How Can i get array in another class:
but usually I get an empty array in the other class
I guess that's because you have more than one instance of the class from which you get your array.
Please post your code. Passing data from one object to another is actually a trivial exercise. -
// classeA.h std::array<QString,25> Inputs; // ClassA.cpp "UploadCSV" void UploadCSV::on_parambtn_2_clicked() { int compteur=0; QString cell0; QString cell3 ; for(int i=0;i<ui->tablewidget->rowCount();i++){ QTableWidgetItem *cell7 = ui->tablewidget->item(i,7); QVariant myData7 = cell7->data(Qt::DisplayRole); if (myData7=="Core_meas"){ cell0 = ui->tablewidget->item(i,0)->text(); cell3 = ui->tablewidget->item(i,3)->text(); Inputs[compteur]=cell3; qDebug() << "Inputs baby"<<Inputs[compteur] ; compteur++; } } }
//in classB.
UploadCSV *up;
//in classB.cpp
up=new UploadCSV(); for(size_t i=0;i<up.Inputs->size();i++){ qqDebug() << "this is data",up.Inputs[i]
-
// classeA.h std::array<QString,25> Inputs; // ClassA.cpp "UploadCSV" void UploadCSV::on_parambtn_2_clicked() { int compteur=0; QString cell0; QString cell3 ; for(int i=0;i<ui->tablewidget->rowCount();i++){ QTableWidgetItem *cell7 = ui->tablewidget->item(i,7); QVariant myData7 = cell7->data(Qt::DisplayRole); if (myData7=="Core_meas"){ cell0 = ui->tablewidget->item(i,0)->text(); cell3 = ui->tablewidget->item(i,3)->text(); Inputs[compteur]=cell3; qDebug() << "Inputs baby"<<Inputs[compteur] ; compteur++; } } }
//in classB.
UploadCSV *up;
//in classB.cpp
up=new UploadCSV(); for(size_t i=0;i<up.Inputs->size();i++){ qqDebug() << "this is data",up.Inputs[i]
@dziko147 Sorry, I fail to understand the code you posted.
What type is myclasseA? And why do you access Inputs from two different objects (up and myclasseA)?
It really looks like you have two different instances of UploadCSV... -
// classeA.h std::array<QString,25> Inputs; // ClassA.cpp "UploadCSV" void UploadCSV::on_parambtn_2_clicked() { int compteur=0; QString cell0; QString cell3 ; for(int i=0;i<ui->tablewidget->rowCount();i++){ QTableWidgetItem *cell7 = ui->tablewidget->item(i,7); QVariant myData7 = cell7->data(Qt::DisplayRole); if (myData7=="Core_meas"){ cell0 = ui->tablewidget->item(i,0)->text(); cell3 = ui->tablewidget->item(i,3)->text(); Inputs[compteur]=cell3; qDebug() << "Inputs baby"<<Inputs[compteur] ; compteur++; } } }
//in classB.
UploadCSV *up;
//in classB.cpp
up=new UploadCSV(); for(size_t i=0;i<up.Inputs->size();i++){ qqDebug() << "this is data",up.Inputs[i]
-
@dziko147 said in How Can i get array in another class:
,myclasseA.Inputs[i]
Why do you have
myClasseA
there, when the object isup
Tryup.Inputs[i]
(Corrected, thx @jsulm )
@Pl45m4 said in How Can i get array in another class:
when the object is UploadCSV
UploadCSV seems to be the class, not object
-
// classeA.h std::array<QString,25> Inputs; // ClassA.cpp "UploadCSV" void UploadCSV::on_parambtn_2_clicked() { int compteur=0; QString cell0; QString cell3 ; for(int i=0;i<ui->tablewidget->rowCount();i++){ QTableWidgetItem *cell7 = ui->tablewidget->item(i,7); QVariant myData7 = cell7->data(Qt::DisplayRole); if (myData7=="Core_meas"){ cell0 = ui->tablewidget->item(i,0)->text(); cell3 = ui->tablewidget->item(i,3)->text(); Inputs[compteur]=cell3; qDebug() << "Inputs baby"<<Inputs[compteur] ; compteur++; } } }
//in classB.
UploadCSV *up;
//in classB.cpp
up=new UploadCSV(); for(size_t i=0;i<up.Inputs->size();i++){ qqDebug() << "this is data",up.Inputs[i]
-
@jsulm sorry I edited the code .
I gave the expamle of classA and ClassB to be clearer .
ClassA (UploadCSV ) is a Qwidget : Qdialog
ClassB (Backend) is QObject .
@dziko147 Is this your real code:
up=new UploadCSV(); for(size_t i=0;i<up.Inputs->size();i++){ qqDebug() << "this is data",up.Inputs[i]
?
Because Items is filled when UploadCSV::on_parambtn_2_clicked() is called (at least according to code you posted). But you're printing Inputs just after creating UploadCSV instance (up)! So why should there be anything in Inputs? -
@CP71 it's called in UploadCSV class (classA) .
and inside this function I set data in Inputs[] .
@dziko147 said in How Can i get array in another class:
it's called in UploadCSV class (classA) .
Where exactly?! And what is classA?! This all is really confusing!
up != classA -
@dziko147 Is this your real code:
up=new UploadCSV(); for(size_t i=0;i<up.Inputs->size();i++){ qqDebug() << "this is data",up.Inputs[i]
?
Because Items is filled when UploadCSV::on_parambtn_2_clicked() is called (at least according to code you posted). But you're printing Inputs just after creating UploadCSV instance (up)! So why should there be anything in Inputs? -
@dziko147 said in How Can i get array in another class:
@jsulm yes i understand you . So can you suggest a way to do this correctly ?
Something like it was explained in your other thread ?
-
@dziko147 said in How Can i get array in another class:
it's called in UploadCSV class (classA) .
Where exactly?! And what is classA?! This all is really confusing!
up != classA -
@jsulm forget classA and classB example .
So curretly we have UploadCSV class and BackendClass .
I would like to get array defined from UploadCSV in Backend class .@dziko147 said in How Can i get array in another class:
I would like to get array defined from UploadCSV in Backend class
Then do so.
To get it you need the UploadCSV instance. How you get it in Backend I don't know as I don't have your code. There are different ways to do this. You can pass pointer to UploadCSV instance to Backend constructor for example.
But to be able to give a good advice we would need to know how you manage your UploadCSV and Backend instances. -
@jsulm
currently I pass a pointer like this .
in backend.hUploadCSV *up;
in backend.cpp
up = new UploadCSV();
-
@jsulm
currently I pass a pointer like this .
in backend.hUploadCSV *up;
in backend.cpp
up = new UploadCSV();
@dziko147
Sorry, but my English is no good.
So maybe I can help you with an example.To link my example.
I think you are filling your array in obj1, but when you try to read it you use obj2.
You that know your project, have a sense what I write?In short, these are possible ways to have your result:
- two or more instance of you class
- your object never fills the array
- your object fills the array but with an empty value
- in somewhere of your class clear your array before you use it
But in your case seems you have two instances of class