Accessing value of vector only once
-
Hello, I have a Question.
For example:
void loadfrom() loads values from file into a vectorI have another function MainWindow::on_start_clicked()
When this Function get called (here via button),
values should load in vec.QVector<QString> vec = loadfrom();
And in another function
MainWindow::on_answer_returned()
At the moment everytime this function get called the vector loads all values everytime. But I want access the vector in this function from MainWindow::on_start_clicked()How can I do this?
-
@MEsc said in Accessing value of vector only once:
I have another function MainWindow::on_start_clicked()
When this Function get called (here via button),
values should load in vec.
QVector<QString> vec = loadfrom();That looks like
vec
is a variable local to the slot attached to the button. You load it and then it is destroyed when the slot exits. If you want thevec
object to have a longer lifetime then you need to arrange that. As @Christian-Ehrlicher says, makingvec
a private member variable of the MainWindow class is probably the right approach. So,class MainWindow: public QMainWindow { ... private: QVector<QString> m_vec };
void MainWindow::on_start_clicked() { ... m_vec = loadfrom(); ... } void MainWindow::on_answer_returned() { ... // do stuff with m_vec ... }
This is basic C++ knowledge and not Qt specific.
-
@MEsc said in Accessing value of vector only once:
How can I do this?
Do not load it again when it's already loaded I would guess. Maybe by storing it in a member variable?
-
@MEsc said in Accessing value of vector only once:
I have another function MainWindow::on_start_clicked()
When this Function get called (here via button),
values should load in vec.
QVector<QString> vec = loadfrom();That looks like
vec
is a variable local to the slot attached to the button. You load it and then it is destroyed when the slot exits. If you want thevec
object to have a longer lifetime then you need to arrange that. As @Christian-Ehrlicher says, makingvec
a private member variable of the MainWindow class is probably the right approach. So,class MainWindow: public QMainWindow { ... private: QVector<QString> m_vec };
void MainWindow::on_start_clicked() { ... m_vec = loadfrom(); ... } void MainWindow::on_answer_returned() { ... // do stuff with m_vec ... }
This is basic C++ knowledge and not Qt specific.