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. How to check if a signal is triggered from GUI or Code

How to check if a signal is triggered from GUI or Code

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 1.8k 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.
  • V Offline
    V Offline
    Vinoth Rajendran4
    wrote on last edited by Vinoth Rajendran4
    #1

    I have a scenario, where i have process a signal triggered from GUI and block the same signal if triggered from Code base.

    Eg: I am subclassing QListWIdget,

    In Constructor

    connect(this,SIGNAL(itemChanged(QListWidgetItem*)),this,SLOT(itemChangedSlot(QListWidgetItem*)));
    }
    

    In this case, the following slot gets called if changes done on ListItem in UI, as well as when setText() is called from code

    void myListWidget::itemChangedSlot(QListWidgetItem * item)
    {
                if(.....) {
                 ....
                 } else {
                            item->setText("hello"));
                 }
    }
    

    Is there a way to find, if particular signal is triggered from UI or from code ??

    My requirement is that i need to process the signal only if its from UI, and block the signal from calling the slot if its from code.

    Any help is appreciated.

    Thanks for reading.

    raven-worxR 1 Reply Last reply
    0
    • V Vinoth Rajendran4

      I have a scenario, where i have process a signal triggered from GUI and block the same signal if triggered from Code base.

      Eg: I am subclassing QListWIdget,

      In Constructor

      connect(this,SIGNAL(itemChanged(QListWidgetItem*)),this,SLOT(itemChangedSlot(QListWidgetItem*)));
      }
      

      In this case, the following slot gets called if changes done on ListItem in UI, as well as when setText() is called from code

      void myListWidget::itemChangedSlot(QListWidgetItem * item)
      {
                  if(.....) {
                   ....
                   } else {
                              item->setText("hello"));
                   }
      }
      

      Is there a way to find, if particular signal is triggered from UI or from code ??

      My requirement is that i need to process the signal only if its from UI, and block the signal from calling the slot if its from code.

      Any help is appreciated.

      Thanks for reading.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Vinoth-Rajendran4 said in How to check if a signal is triggered from GUI or Code:

      My requirement is that i need to process the signal only if its from UI, and block the signal from calling the slot if its from code.

      1. set a member flag variable before you execute code that triggers the signal which you can check in your slot, and revert it after the code
      2. call QObject::blockSignals() before and after the your code which triggers the signal, but note that this blocks all signals of the object. Depending on the object/signal this might have side effects for Qt's internal code

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      V 1 Reply Last reply
      3
      • raven-worxR raven-worx

        @Vinoth-Rajendran4 said in How to check if a signal is triggered from GUI or Code:

        My requirement is that i need to process the signal only if its from UI, and block the signal from calling the slot if its from code.

        1. set a member flag variable before you execute code that triggers the signal which you can check in your slot, and revert it after the code
        2. call QObject::blockSignals() before and after the your code which triggers the signal, but note that this blocks all signals of the object. Depending on the object/signal this might have side effects for Qt's internal code
        V Offline
        V Offline
        Vinoth Rajendran4
        wrote on last edited by Vinoth Rajendran4
        #3

        @raven-worx : Tried QObject::blockSignals(). Suits my need.
        Thank u

        1 Reply Last reply
        0
        • V Offline
          V Offline
          Vinoth Rajendran4
          wrote on last edited by
          #4

          @raven-worx : But is there any way to find out on entry of slot function, that particular signal is triggered from UI or code ??

          raven-worxR KroMignonK 2 Replies Last reply
          0
          • V Vinoth Rajendran4

            @raven-worx : But is there any way to find out on entry of slot function, that particular signal is triggered from UI or code ??

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by raven-worx
            #5

            @Vinoth-Rajendran4
            no not really.
            In the end everything is triggered from code ;)
            "From UI" mostly means that somewhere in the call stack a (input-)event caused the code to be executed.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            2
            • V Vinoth Rajendran4

              @raven-worx : But is there any way to find out on entry of slot function, that particular signal is triggered from UI or code ??

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by
              #6

              @Vinoth-Rajendran4 said in How to check if a signal is triggered from GUI or Code:

              But is there any way to find out on entry of slot function, that particular signal is triggered from UI or code ??

              It is possible to know which was the signal sender with sender() and the signal index with senderSignalIndex()

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              1 Reply Last reply
              1
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                If you mean "direct call" VS "signal/slots" then the sender() return value will be 0 in the first case and whatever sent the signal in the second case.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1

                • Login

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