Results 1 to 18 of 18

Thread: filter signal to limited slots

  1. #1
    Join Date
    Mar 2009
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Question filter signal to limited slots

    Hi,

    Is it possible to filter a common signal such that only few of the many clients who have connected to the signal receive them.

    For example:

    class customSignalling : public QObject
    {
    Q_OBJECT

    public:
    customSignalling(QObject* aParent);
    register();

    signals:
    void sendSignal();

    }


    Here lets say classes A, B and C have connected to the signal
    like

    connect(customSignalling, SIGNAL(sendSignal()), this, SLOT(slot())),

    Now can I limit the signal from being to C but A and B could receive it.
    any permutation of it can I control the class objects which will receive.
    Or is it a pure Broadcast.

    Please advise.

    Regards,
    Srikanth

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: filter signal to limited slots

    Hi,

    Disconnect them
    Òscar Llarch i Galán

  3. #3
    Join Date
    Mar 2009
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: filter signal to limited slots

    I am sorry I didnot understand your solution.
    Are you asking to use QObject::disconnect api.

    Actually i am looking for a temporary disable and re-enabling the
    connection is there any such possibility.

    regards,
    Srikanth

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: filter signal to limited slots

    Quote Originally Posted by sriky27 View Post
    I am sorry I didnot understand your solution.
    Are you asking to use QObject::disconnect api.

    Actually i am looking for a temporary disable and re-enabling the
    connection is there any such possibility.

    regards,
    Srikanth
    take a look at QObject::blockSignals.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: filter signal to limited slots

    Hi,

    Do you know if there is performace difference using "blockSignals" or disconnecting them?

    I am sorry I didnot understand your solution.
    Are you asking to use QObject::disconnect api.
    Yes, I think on disconnecting and reconnecting them when needed.
    Also you can use "blockSignals" as spirit told you.
    Òscar Llarch i Galán

  6. #6
    Join Date
    Mar 2009
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: filter signal to limited slots

    Hi everyone,

    Thanks for your suggestions, I have indeed gone through blocksignals,
    i want to blocksignals to specific receivers, how to do that.

    Regards,
    Srikanth

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: filter signal to limited slots

    Hi, blockSignals is no alternative to dis-/reconnect (in this case)

    SENDER = emits the signal
    A, B, C are receivers.

    When setting SENDER.blockSignals(true) none of A,B,C will receive the signal. But as I have sriky27 understood, only e.g. B should not receive. So an special dis-/reconnect for B is necessary. Or you implement an member to A,B,C like "processSignal". (If A,B,C has an abstract base class I would prefer that instead of every time dis- and reconnect. If you need it often.)

    Qt Code:
    1. void XXX::mySlot() {
    2. if(!processSignal)
    3. return;
    4. //...
    5. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: filter signal to limited slots

    try this
    Qt Code:
    1. ...
    2. const bool isBlocked = receiver->blockSignals(true);
    3. //some actions
    4. receiver->blockSignals(isBlocked);
    5. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  9. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: filter signal to limited slots

    Quote Originally Posted by spirit View Post
    Qt Code:
    1. receiver->blockSignals(true);
    To copy to clipboard, switch view to plain text mode 
    only avoid receiver to send signals, not to receive signals and execute slots

  10. #10
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: filter signal to limited slots

    yep, my mistake, receiver must be replaced by sender.
    but it will block ALL signals as was posted above.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  11. #11
    Join Date
    Mar 2009
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: filter signal to limited slots

    Hi everyone,

    What I am trying to ask is: whether it is possible to send signals to few receivers only and block the rest. Is there any blockSignals(receievers, bool); api. How to do that??

    Regards,
    Sriky

  12. #12
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: filter signal to limited slots

    hm, why you can't connect needed object with proper slots and don't connect another set of objects?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  13. #13
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: filter signal to limited slots

    Is there any blockSignals(receievers, bool); api.
    No.

    How to do that?
    Dis-/reconnect the classes by yourself (on runtime), or add a switch - as above mentioned - to your classes, which decides if the slot should be executed or not.

  14. #14
    Join Date
    Mar 2009
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: filter signal to limited slots

    Hi everyone,

    I am writing an interface, where there is a common signal exposed sth like

    class xxxx
    {
    register()

    signals:
    void doSomething();
    };

    Now there are many registered objects for the signal but only few of the objects should be notified.

    Which is normally it is possible with asking the registered classes to implement an interface using which I could call the interface function of the specified objects I want.
    That is the solution.
    sth like

    class interface
    {
    virtual void doSomething()=0;
    ]

    class A: public interface
    {
    void doSomething();
    }

    class B: public interface
    {
    void doSomething();
    }


    class C: public interface
    {
    void doSomething();
    }

    when condition arises
    Now having maintained the list of the objects
    in QList<registeredClass> list;

    for(;
    {
    if()
    {
    list.doSomething();
    }
    }


    But as I don't want to use the above ,as Qt provides me with signal and slot mechanism,I want to use them and restrict the signal to only few of the many slots.

    Regards,
    Srikanth

  15. #15
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: filter signal to limited slots

    Qt Code:
    1. class interface
    2. {
    3. virtual void doSomething()=0;
    4. void setExecSlot(bool exec) {execSlot = exec;}
    5. private:
    6. bool m_execSlot;
    7. }
    8.  
    9. class B: public interface
    10. {
    11. void doSomething() {if (!m_execSlot){return;} /*other stuff...*/}
    12. }
    13.  
    14. //...
    15. ptrToB->setExecSlot(false);
    16. //...
    17. ptrToB->setExecSlot(true);
    To copy to clipboard, switch view to plain text mode 

    There you have your "blockSignals(receievers, bool); api."

    or

    send a signal with an parameter which indicates which classes should "respect" the signal.

  16. #16
    Join Date
    Mar 2009
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: filter signal to limited slots

    Hi,

    Thanks for your replies. The point I want to make it QObject has the list of all receivers, I just don't know how to get the receivers list or set the receiver list. Now I have to do the same book keeping that QObject does. I somehow not very much happy with the approach.

    Regards,
    Srikanth

  17. #17
    Join Date
    Mar 2009
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: filter signal to limited slots

    Quote Originally Posted by Lykurg View Post
    No.


    Dis-/reconnect the classes by yourself (on runtime), or add a switch - as above mentioned - to your classes, which decides if the slot should be executed or not.

    Hi,

    How do you reconnect? In the class which emits the signals I donot know the slot of the receiver. If I have to do that I have to know upfront the type of the receivers which implement the slots would be? Please correct me if I am wrong.

    Regards,
    Srikanth

  18. #18
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: filter signal to limited slots

    Well, at any point of your application you establish the connection via QObject::connect. In that class you - normally - have to do your reconnect.

Similar Threads

  1. Queuing problem with signal and slots
    By montylee in forum Qt Programming
    Replies: 4
    Last Post: 20th November 2009, 06:11
  2. How to Implement signal and slots for QAccessibleWidget
    By Rakesh_Kumar in forum Qt Programming
    Replies: 1
    Last Post: 30th January 2009, 06:57
  3. pthread instead QThread
    By brevleq in forum Qt Programming
    Replies: 8
    Last Post: 23rd December 2008, 07:16
  4. Connection of custon signals/slots
    By brevleq in forum Qt Programming
    Replies: 2
    Last Post: 23rd December 2008, 07:04
  5. Signal and slots
    By villy in forum Qt Programming
    Replies: 1
    Last Post: 12th January 2007, 10:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.