Unsolved How Can i get array in another class
-
@CP71 thanks for your reply .
I used your solution but still get array empty.I set data in Inputs in a Slot called after clicking in button .
It cause problem ?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++; } } }
-
@dziko147
I suppose you see a valid values in array at line:
qDebug () << "Inputs baby" << Inputs [compteur];If yes, the only thing I think is a double instance of the class, but surely you have already checked it.
I don't have other ideas :(
-
@dziko147 At current state of the project I need only a text (QTString ) - basically address and name of local , connected to PC, Bluetooth device.
That data is a result of my dialog "scan" for local Bluetooth adapters. .Since it is such small amount of data I am currently NOT using "connect".
Eventually I am going to use connect to pass real data from the dialog to the form.I started using "go to slot" feature , however I have not figured out how to use it "between objects ".
It is interesting feature but so far only good to "pass data" in same object.It has an optional "parameter", but I not found how to use it.
So in summary - using "go to slot" should be able to pass data between objects - as you desire . But I need to study it more.
PS
Passing data between objects seems to be subject of many discussions.
The way I see it - it is two step process - you have to collect the data (obviously) using "connect" triggered by the data event. . Then use second "connect " to pass the captured data between objects. -
@AnneRanch if you need just a Qstring to pass it . I recommend using signal/Slot .
-
@dziko147 you should go back to the basics first: séparation of responsibilities.
There's no reason for you child class to have an instance of your GUI class in it.
From the looks of it, your GUI is responsible to generate that array and it looks as well as the class processing that array should receive it from the GUI object. Hence there's no need for it to know anything or contain an object of your GUI class.
-
@SGaist exact. I set data in my array from GUI element .
if i understand you. I don't need to create an instance of my class .
So how can i call the array from the other class -
If class B is child of class A, then you can do sth like this in class B
classA *clsA= dynamic_cast <classA*> (parent()); clsA->funcToGetData();
-
@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:
And if classA isn't child od classB . How can i call it
The code you were shown attempts to cast the
parent()
toclassA
. If yourclassA
instance is not related to youclassB
instance through its parent, then use whatever you have for the desiredclassA
instance....I'm not sure but you seem not to understand the difference between a class and an instance (or object)? If that is the case I suggest you read up to understand, as this is vital in OO programming.
-
@JonB no i know the difference between instance and class .
But honestly i have no idea what parent() mean .
And if we say a classA is a child of classB we mean classA is inherited drom classB .Thank you
-
@dziko147 said in How Can i get array in another class:
we mean classA is inherited drom classB
No, we don't.
See https://doc.qt.io/qt-5/objecttrees.html
Parent/child is not about inheritance it is about relationships between QObject based classes. -
@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]
-
@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... -
@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
-
@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 said in How Can i get array in another class:
up=new UploadCSV();
for(size_t i=0;i<up.Inputs->size();i++){
qqDebug() << "this is data",up.Inputs[i]Sorry, who calls the on_parambtn_2_clicked() function?