[How to use a result of a function in another class]
-
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; }
-