Suppress a single Signal



  • Hi Everyone!

    Does anyone know how to suppress a single signal from a class. For example say Class A has the signal DataChanged(int). What if sometimes you don't want to receive that signal at ClassB. So far I've been doing the standard:

    disconnect(ObjectA, 
    SIGNAL(DataChanged(int)),
    this,
     SLOT(someSlot()));
    
    /* do some work */
    
    connect(ObjectA, 
    SIGNAL(DataChanged(int)),
    this, 
    SLOT(someSlot()));
    

    I'm just curious if there is a way to say "suppress DataChanged(int)."



  • Hi,

    Maybe you could use the blockSignals method to temporary deactivate the signals of your class. Please note that this method deactivate all the signals of a class and not a specific one.

    bool oldState = ObjectA->blockSignals(true);
    // do some stuff 
    ObjectA->blockSignals(oldState);
    

    Regards,
    Vincent



  • To add to @marievin take a look at QSignalBlocker



  • @VRonin QSignalBlocker is something similar to what I'm looking for; however, blocksSignals is something that I'm not looking to do as there are a few signals certain classes still need to emit. QSignalBlocker is not available in our version of QT. We are using a version less than Qt 5.3. I wish there was a "suppress" keyword of some sort. I think we will be stuck to the situation I demonstrated above.


  • Lifetime Qt Champion

    Hi,

    QSignalBlocker is easily re-implemented but from your description it looks rather the other way around. The slot you connected to that signal should not execute. If so, shouldn't you have a boolean enabler that you check at the start of your slot ?



  • @SGaist As I mentioned above, unfortunately, we do not have access to QSignalBlocker as it is not in our version of QT. When I do the disconnect and then reconnect, the time in between the slot is not executing like I intend. However, what I am asking is if there is a way other than QSignalBlocker and disconnect/reconnect to block a specific signal from an object. For example, the code might look something like suppress dataChanged();



  • QObject::blockSignals should be available to you in whatever version of Qt but this blocks all signals emitted from the object.

    Are you sure you are not just looking for an easy fix to a design flaw?



  • @VRonin I'm not entirely sure how to respond to the - "Are you sure you are not just looking for an easy fix to a design flaw?" However, I will say that this is a massive project with thousands of signals being sent and received by hundreds of different objects all deriving data from data model singletons. So let's look at your solution "QObject::blockSignals" as I'll repeat numerous times, there are certain signals that need to continue being sent and perhaps one or two that need to be muted by a specific listener class. Your solution would prevent any display widget, or perhaps other models, from receiving data updates from the model because you are blocking ALL signals. My question was quite straight forward, I thought, when asking if there was another approach in suppressing a single signal from a class when other signals are needed as they are feeding data to other systems, but perhaps I was wrong. Gathering from your responses, there is no set way to do this other than disconnecting the slot, doing the work, and then reconnecting that slot. I appreciate the input.



  • Sorry, I realise I was not clear enough.

    there are certain signals that need to continue being sent and perhaps one or two that need to be muted by a specific listener class.

    No, only disconnect would do this

    Are you sure you are not just looking for an easy fix to a design flaw?

    What I meant is: are you sure you want to block the signal rather than having a flag in the slot?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.