Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using flags to stop triggered actions

Using flags to stop triggered actions

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 619 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Dummie1138D Offline
    Dummie1138D Offline
    Dummie1138
    wrote on last edited by
    #1

    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.

    Pl45m4P 1 Reply Last reply
    0
    • Dummie1138D Dummie1138

      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.

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @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 like

      void 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. allowToChangeConnection and 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
      }
      
      

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      Dummie1138D 1 Reply Last reply
      2
      • Dummie1138D Dummie1138 has marked this topic as solved on
      • Pl45m4P Pl45m4

        @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 like

        void 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. allowToChangeConnection and 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
        }
        
        
        Dummie1138D Offline
        Dummie1138D Offline
        Dummie1138
        wrote on last edited by
        #3

        @Pl45m4

        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.

        JonBJ Pl45m4P 2 Replies Last reply
        0
        • Dummie1138D Dummie1138

          @Pl45m4

          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.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Dummie1138
          What "temporary value"? It receives the new value in val. Then it checks current value in this->val and 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)
              }
          
          1 Reply Last reply
          0
          • Dummie1138D Dummie1138

            @Pl45m4

            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.

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by
            #5

            @Dummie1138

            This is useful when having something like a slider, for example.
            Every movement of the handle triggers valueChanged.
            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 is 42 and 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.


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            JonBJ 1 Reply Last reply
            0
            • Pl45m4P Pl45m4

              @Dummie1138

              This is useful when having something like a slider, for example.
              Every movement of the handle triggers valueChanged.
              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 is 42 and 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.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Pl45m4
              If you are referring to your code snippet as shown

                  if(this->val != val)
                      this->val = val;
              

              then the if doesn'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.

              Pl45m4P 1 Reply Last reply
              0
              • JonBJ JonB

                @Pl45m4
                If you are referring to your code snippet as shown

                    if(this->val != val)
                        this->val = val;
                

                then the if doesn'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.

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by Pl45m4
                #7

                @JonB

                I picked the wrong function name :)
                It should be some function to set val somewhere in the code, do the checking and then emit the signal and not a function which is the slot connected to changed.
                Edited my initial post :)


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved