[How to use a result of a function in another class]
-
Hello guys,
I hope you're all doing well.So I've been wondering how I can use a variable that belongs to a certain class in another class.
In fact in my MainWindow class, I have a function that returns a QList of floats that I want to use in my MainWidget class so here is what I tried to do although there must be something wrong because I don't get the right result.
In my MainWindow class, I added a getter function that returns the QList.
Then in my MainWidget class I created an instance of MainWindow so I can call the getter function as shown in the code below:
MainWindow wind; wind->getvariableA() qDebug()<<"variable"<<wind->getvariable() // to see what result I get
I'm supposed to see the values of the QList however All I get is some empty QLists.
I know that I'm doing something wrong but I can't tell what it is.
Please Help me.
Any little help would be appreciated and thank you.
-
@appdev said in [How to use a result of a function in another class]:
Then in my MainWidget class I created an instance of MainWindow
Why?
You should use the MainWindow instance you already have instead of creating a new one.There is another problem with what you are doing: children should not know anything about their parents, only other way around! I assume here that MainWidget is a child of MainWindow.
-
@appdev I'm not talking about inheritance, I'm talking about child/parent concept in Qt: https://doc.qt.io/qt-5/objecttrees.html
-
Okay, I have to read the document you just shared.
But I have a suggestion.
What if I create another class in which I develop the function that returns the QList of floats and some other functions that I need and of course the getter function and then call the getter in MainWidget.That woudn't be a problem right ?
-
@appdev said in [How to use a result of a function in another class]:
That woudn't be a problem right ?
This is just fine. Especially if this data/functionality is not directly related to UI. But you have to make sure you use the correct instance instead of creating new one each time you want to call the getter!
-
Well I don't think it's related directly to the UI.
In fact, here is the thing.
In my mainwindow.ui, I added a push button which when clicked, it opens a file, reads it and extracts the values then stores them in QLists. All these functions were developed in my MainWindow.cpp since when I right click the button and go to slot, it takes me directly to the mainwindow.cpp file.
So I'm going to create another class, develop these functions again ( read the file, extract the values, store them in QList) and in my MainWindow class I'm gonna connect the button to the new class.
Then, I'm going to add the getter to the new class and see if it works that way.
-
Hello again.
So you said that children shouldn't know anything about their parents but the other way is possible. So I tried another thing:
I developped another function that copies the QList values into another list which is an attribute of the mainwidget class.
void MainWindow::copyvaluestomainwidget() { for (int i=0; i<w_list.size();i++) { this->ui->mainwidget->w.append(w_list[i]); } qDebug()<<"w"<<this->ui->mainwidget->w; }
I used qDebug() inside this function to see if the list is empty or not but didn't get anything. It looks like qDebug() isn't working and I can't figure out why. However
It works inside other functions. -
Hello again,
void MainWindow::copyvaluestomainwidget() { for (int i=0; i<w_list.size();i++) { this->ui->mainwidget->w.append(w_list[i]); } qDebug()<<"w"<<this->ui->mainwidget->w; }
This function copies the values of the QList "w_list" in another QList which is "w".
w_list is an attribute of mainwindow class.
w is an attribute of mainwidget class.@SGaist @JonB The copied list isn't empty because when I call that function in the other function that opens the file, extracts data and stores them in QLists and use qDebug() as follows:
copyw_valuestomainwidget(); qDebug()<<"w"<<this->ui->mainwidget->w;
The output of the application shows "w" filled with the copied values.
However, there is something wrong : I declared "w" as QList of floats in the mainwidget class attributes.
And tried to fill it with values of a QList from the mainwindow class which I called w_list.
When I go to mainwidget class and try to see the values of "w" [the copied list], I still get an empty list. I didn't know what's missing.
-
@appdev said in [How to use a result of a function in another class]:
I used qDebug() inside this function to see if the list is empty or not but didn't get anything. It looks like qDebug() isn't working and I can't figure out why.
So you did not "didn't get anything", you got at least
"w"
printed out from theqDebug()
to show it has been hit? Can you please be accurate on what you say, we can only go by what you write.It looks like qDebug() isn't working
qDebug()
"works".I don't know what you are doing wrong. For all I know there is a "shadow" local variable for
w
orw_list
hiding the member variable maybe....void MainWindow::copyvaluestomainwidget() { qDebug() << w_list.size() << this->ui->mainwidget->w.size(); for (int i=0; i<w_list.size();i++) { qDebug() << w_list[i]; this->ui->mainwidget->w.append(w_list[i]); } qDebug() << w_list.size() << this->ui->mainwidget->w.size(); qDebug()<<"w"<<this->ui->mainwidget->w; }
Then there is your:
copyw_valuestomainwidget();
Well, the method you show is named
MainWindow::copyvaluestomainwidget()
. So that call does not call it, or you are not showing us the relevant code. -
Okay, I will try to be more accurate.
In my MainWindow class, I have two functions:
-One that opens a file, extracts some data, stores these data in QLists ( 4 QLists to be more precise)
-the second one copies the values of the first QList in a new list.This is the second function:
void MainWindow::copyw_valuestomainwidget() { for (int i=0; i<w_list.size();i++) { this->ui->mainwidget->w.append(w_list[i]); } qDebug()<<"w"<<this->ui->mainwidget->w; }
When I run my program, the output of the application doesn't show anything even if i add qDebug inside that function as I did in the code above.
When I call the copy function in the function that opens the file, extracts data and stores them in QLists and then add qDebug() to see if the new list is full, the output of the application shows the new list which is successfully filled.
copyw_valuestomainwidget(); qDebug()<<"w"<<this->ui->mainwidget->w;
That's how I call the copy function and I do see the new QList which is "w" and which filled with the same values as the old QList "w_list".
Till now, I'm only talking about the mainwindow class.
Now here is the thing:w_list is an attribute of mainwindow.
w is an attribute of mainwidget.
My goal is to copy the values from the QList "w_list" to the new QList "w" so I can use the values of "w" in the class mainwidget.After developping the copy function in the class mainwindow, I went to mainwidget class and tried this:
qDebug()<<"w"<<w;
The output of the application shows an empty QList in this case, as if the application didn't take into consideration the copy function I used to fill the QList w.
I didn't know what's missing
-
@appdev said in [How to use a result of a function in another class]:
After developping the copy function in the class mainwindow, I went to mainwidget class and tried this:
qDebug()<<"w"<<w;- Please show the whole code where you put this qDebug
- Do you call this qDebug AFTER doing the copy?
One note to the design: You should NOT implement the copy inside MainWindow. Instead your mainwidget class should have a setter method like:
void mainwidget::setList(const QList<WHATEVERTYPEITIS> list) { w += list; }
-