How to access variable from one slot to another slot
-
Hi,
I just started working QT and I need a very small help.
So basically I have 2 pushbuttons.
In the first slot(when pushbutton 1 is clicked), I'm storing values inside a QVector of the size of which the user enters.
I need to access this QVector in the second pushbutton slot.void QtSortArray::on_generateArray_clicked() {int arraySize_value = ui->lineEditArraySize->text().toInt(); QVector<double> actualVector(arraySize_value); for (int i = 0; i < actualVector.size(); i++) { actualVector[i] = distribution(*QRandomGenerator::global()); ui->inputArray->addItem(QString::number(actualVector[i])); } void QtSortArray::on_pushbutton2_clicked() { qDebug() << actualVector.size() ; }So I want to access the actualVector elements in the on_pushbutton2_clicked slot. Any suggestions on how to do it?
-
Hi,
I just started working QT and I need a very small help.
So basically I have 2 pushbuttons.
In the first slot(when pushbutton 1 is clicked), I'm storing values inside a QVector of the size of which the user enters.
I need to access this QVector in the second pushbutton slot.void QtSortArray::on_generateArray_clicked() {int arraySize_value = ui->lineEditArraySize->text().toInt(); QVector<double> actualVector(arraySize_value); for (int i = 0; i < actualVector.size(); i++) { actualVector[i] = distribution(*QRandomGenerator::global()); ui->inputArray->addItem(QString::number(actualVector[i])); } void QtSortArray::on_pushbutton2_clicked() { qDebug() << actualVector.size() ; }So I want to access the actualVector elements in the on_pushbutton2_clicked slot. Any suggestions on how to do it?
@tsdk
So makeactualVectora member variable of yourclass QtSortArray, so that it can be accessed by any method of the class, including any slots. That is probably what you want here, though another approach is to access the items you have put intoui->inputArray(you might have to convert them back from string to number), which is available from the slot, instead of inactualVector.BTW, don't start out naming your own class as
Qt...or evenQ...anything. Leave that for Qt's own classes. Pick some other prefix which relates to your own app instead. -
@tsdk
So makeactualVectora member variable of yourclass QtSortArray, so that it can be accessed by any method of the class, including any slots. That is probably what you want here, though another approach is to access the items you have put intoui->inputArray(you might have to convert them back from string to number), which is available from the slot, instead of inactualVector.BTW, don't start out naming your own class as
Qt...or evenQ...anything. Leave that for Qt's own classes. Pick some other prefix which relates to your own app instead.@JonB said in How to access variable from one slot to another slot:
actualVector
Thanks a lot. I did define actualVector as a member of class QTSortArray
class QtSortArray : public QMainWindow { Q_OBJECT public: QVector<double> *actualVector;Then I was pointing it to a temporary variable created in one of the slot.
QVector<double> tempVector(arraySize_value); ui->inputArray->clear(); if (arraySize_value > 3) { for (int i = 0; i < tempVector.size(); i++) { tempVector[i] = distribution(*QRandomGenerator::global()); ui->inputArray->addItem(QString::number(tempVector[i])); } actualVector = &tempVector; qDebug()<< actualVector->size();But when I use the same in another slot, still it shows size is 0.
void QtSortArray::on_sortArray_clicked() { { qDebug()<< actualVector->size(); } }Indeed I was thinking of retrieving it from QlistWidget items but I thought if I can just access it directly then it would be more easier. And Thanks, I will name the classes wisely next time.
-
@tsdk said in How to access variable from one slot to another slot:
Then I was pointing it to a temporary variable created in one of the slot.
You already explain why it does not work. You assign the pointer to a temporary which is not available later on. You have to assign the values (and don't use a pointer - it's not needed here) instead.
-
@JonB said in How to access variable from one slot to another slot:
actualVector
Thanks a lot. I did define actualVector as a member of class QTSortArray
class QtSortArray : public QMainWindow { Q_OBJECT public: QVector<double> *actualVector;Then I was pointing it to a temporary variable created in one of the slot.
QVector<double> tempVector(arraySize_value); ui->inputArray->clear(); if (arraySize_value > 3) { for (int i = 0; i < tempVector.size(); i++) { tempVector[i] = distribution(*QRandomGenerator::global()); ui->inputArray->addItem(QString::number(tempVector[i])); } actualVector = &tempVector; qDebug()<< actualVector->size();But when I use the same in another slot, still it shows size is 0.
void QtSortArray::on_sortArray_clicked() { { qDebug()<< actualVector->size(); } }Indeed I was thinking of retrieving it from QlistWidget items but I thought if I can just access it directly then it would be more easier. And Thanks, I will name the classes wisely next time.
-
@tsdk said in How to access variable from one slot to another slot:
Then I was pointing it to a temporary variable created in one of the slot.
You already explain why it does not work. You assign the pointer to a temporary which is not available later on. You have to assign the values (and don't use a pointer - it's not needed here) instead.
@Christian-Ehrlicher said in How to access variable from one slot to another slot:
You already explain why it does not work. You assign the pointer to a temporary which is not available later on.
Ahh I got it. Thanks alot.