Using flags to stop triggered actions
-
Hi. I have the following code.
//called when the device connects to the software. puts the data in the appropraite sections of the main screen void MainWindow::setInformation(){ updating = true; bool duplicate = false; for(int i = 0; i < ui->USBConnectionState->count(); i++){ if(Controller::get()->getSerialNum() == ui->USBConnectionState->itemText(i)){ duplicate = true; break; } } //QString serialNum = Controller::get()->getSerialNum(); if(!duplicate){ ui->USBConnectionState->addItem(Controller::get()->getSerialNum()); int indexCount = ui->USBConnectionState->count(); ui->USBConnectionState->setCurrentIndex(abs(indexCount - 1)); } ui->statusBar->setText("Select an option from the navigation bar"); ui->pushButton_Calibration->setDisabled(false); //Controller::get()->requestPresetComparison(); //Do we need this here? updating = false; } //Sends the controller a message on what the new value is void MainWindow::on_USBConnectionState_currentIndexChanged(const QString &arg1){ if(!updating){ emit changeConnection(arg1); } }The idea is that, in setInformation(), there's some code that would trigger on_USBConnectionState_currentIndexChanged, and changeConnection is connected to something that triggers setInformation. As a result, I have a bool that tells the function to not do its function thing when setInformation is ongoing.
The issue is that I will have to implement 5 other bools like this for other situations. And I am wondering whether there are flags that can be activated that can stop triggered actions, since I don't want to keep track of all the bools, and I don't want my code to look like this:
//Sends the controller a message on what the new value is void MainWindow::on_USBConnectionState_currentIndexChanged(const QString &arg1){ if(!updating || !downloading || !closing || !calling || !connecting || !fooing){ emit changeConnection(arg1); } }I am wondering whether there's a better way of doing this that doesn't rely on bools, but instead on (either) higher functions of C++ or Qt. Please let me know if more information is required.
-
Hi. I have the following code.
//called when the device connects to the software. puts the data in the appropraite sections of the main screen void MainWindow::setInformation(){ updating = true; bool duplicate = false; for(int i = 0; i < ui->USBConnectionState->count(); i++){ if(Controller::get()->getSerialNum() == ui->USBConnectionState->itemText(i)){ duplicate = true; break; } } //QString serialNum = Controller::get()->getSerialNum(); if(!duplicate){ ui->USBConnectionState->addItem(Controller::get()->getSerialNum()); int indexCount = ui->USBConnectionState->count(); ui->USBConnectionState->setCurrentIndex(abs(indexCount - 1)); } ui->statusBar->setText("Select an option from the navigation bar"); ui->pushButton_Calibration->setDisabled(false); //Controller::get()->requestPresetComparison(); //Do we need this here? updating = false; } //Sends the controller a message on what the new value is void MainWindow::on_USBConnectionState_currentIndexChanged(const QString &arg1){ if(!updating){ emit changeConnection(arg1); } }The idea is that, in setInformation(), there's some code that would trigger on_USBConnectionState_currentIndexChanged, and changeConnection is connected to something that triggers setInformation. As a result, I have a bool that tells the function to not do its function thing when setInformation is ongoing.
The issue is that I will have to implement 5 other bools like this for other situations. And I am wondering whether there are flags that can be activated that can stop triggered actions, since I don't want to keep track of all the bools, and I don't want my code to look like this:
//Sends the controller a message on what the new value is void MainWindow::on_USBConnectionState_currentIndexChanged(const QString &arg1){ if(!updating || !downloading || !closing || !calling || !connecting || !fooing){ emit changeConnection(arg1); } }I am wondering whether there's a better way of doing this that doesn't rely on bools, but instead on (either) higher functions of C++ or Qt. Please let me know if more information is required.
@Dummie1138 said in Using flags to stop triggered actions:
The issue is that I will have to implement 5 other bools like this for other situations. And I am wondering whether there are flags that can be activated that can stop triggered actions, since I don't want to keep track of all the bools, and I don't want my code to look like this:
It's a used pratice in signal controlled setters.
Not 5 checks, but something likevoid MainWindow::setVal(const int val){ if(this->val != val){ this->val = val; emit valueChanged(val); } }[ Edit: function changed to "some function which modifies
val]
This can be used to prevent flooding your app with calls, when a value changed temporarily, but ends up with the same value as set before.You could merge all 5 booleans into one, e.g.
allowToChangeConnectionand put them in place where you have the 5 (if your design allows that and you dont need to check for them separately elsewhere)or (it's faster than comparing 5 or more booleans one after another):
// if one of them becomes true, you dont fire your signal if(updating+downloading+closing+calling+connecting+fooing == 0){ emit changeConnection } -
D Dummie1138 has marked this topic as solved on
-
@Dummie1138 said in Using flags to stop triggered actions:
The issue is that I will have to implement 5 other bools like this for other situations. And I am wondering whether there are flags that can be activated that can stop triggered actions, since I don't want to keep track of all the bools, and I don't want my code to look like this:
It's a used pratice in signal controlled setters.
Not 5 checks, but something likevoid MainWindow::setVal(const int val){ if(this->val != val){ this->val = val; emit valueChanged(val); } }[ Edit: function changed to "some function which modifies
val]
This can be used to prevent flooding your app with calls, when a value changed temporarily, but ends up with the same value as set before.You could merge all 5 booleans into one, e.g.
allowToChangeConnectionand put them in place where you have the 5 (if your design allows that and you dont need to check for them separately elsewhere)or (it's faster than comparing 5 or more booleans one after another):
// if one of them becomes true, you dont fire your signal if(updating+downloading+closing+calling+connecting+fooing == 0){ emit changeConnection }void MainWindow::on_valueChanged(const int val){ if(this->val != val) this->val = val; }So, this is here to make sure the object is changed away from it's temporary value before any subsequent code? How would it know to distinguish between a temporary value and a real value?
I will attempt to implement something similar to the example, to the best of my ability.
-
void MainWindow::on_valueChanged(const int val){ if(this->val != val) this->val = val; }So, this is here to make sure the object is changed away from it's temporary value before any subsequent code? How would it know to distinguish between a temporary value and a real value?
I will attempt to implement something similar to the example, to the best of my ability.
@Dummie1138
What "temporary value"? It receives the new value inval. Then it checks current value inthis->valand only bothers to set it if it's different.Why @Pl45m4 is telling you to do this here --- in what looks like a slot? --- I don't know. It is common to use this when emitting a signal, to prevent signal for changed being emitted when the value does not actually change:
if(this->val != val) { this->val = val; emit valueChanged(val) } -
void MainWindow::on_valueChanged(const int val){ if(this->val != val) this->val = val; }So, this is here to make sure the object is changed away from it's temporary value before any subsequent code? How would it know to distinguish between a temporary value and a real value?
I will attempt to implement something similar to the example, to the best of my ability.
This is useful when having something like a slider, for example.
Every movement of the handle triggersvalueChanged.
But in some cases, you want only the final value, when the handle is released. So you dont need to update anything else, if the starting value equals the last value.
Or you "watch" some member variable and have your own signals connected to update it. This member is used to initialize some heavy calculations.
If your value is42and somewhere in your code, something triggers the signal connected to the corresponding slot, passing the same value (42) again, you dont re-calculate everything again.
The signal is coming through (might be important to know), but the actual value wont change. -
This is useful when having something like a slider, for example.
Every movement of the handle triggersvalueChanged.
But in some cases, you want only the final value, when the handle is released. So you dont need to update anything else, if the starting value equals the last value.
Or you "watch" some member variable and have your own signals connected to update it. This member is used to initialize some heavy calculations.
If your value is42and somewhere in your code, something triggers the signal connected to the corresponding slot, passing the same value (42) again, you dont re-calculate everything again.
The signal is coming through (might be important to know), but the actual value wont change.@Pl45m4
If you are referring to your code snippet as shownif(this->val != val) this->val = val;then the
ifdoesn't achieve anything in particular. Since it's only a variable assignment (not a function call) there are no side-effects whether you assign the same value as it currently has or not. Maybe you are talking about something else, or you & @Dummie1138 understand something different. -
@Pl45m4
If you are referring to your code snippet as shownif(this->val != val) this->val = val;then the
ifdoesn't achieve anything in particular. Since it's only a variable assignment (not a function call) there are no side-effects whether you assign the same value as it currently has or not. Maybe you are talking about something else, or you & @Dummie1138 understand something different.