Results 1 to 18 of 18

Thread: how to find all signals connected to specific slot

  1. #1
    Join Date
    Dec 2009
    Posts
    65
    Thanks
    10
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default how to find all signals connected to specific slot

    Hi
    Here is the problem. i have a class for a device, with a signal "send(QByteArray)" and slot "receive(QByteArray)". There are a lot of other devices that could connect their send signal with another receive slot. I am trying to "monitor" communication for any chosen device. my idea was to intercept its send and receive functions - connect my monitor's sent slot to device's send signal, and to connect all signals connected to device's slot receive to monitor's slot received. What seams to be hard is to find all signals connected to specific slot. I don't need to know about senders, i just want to intercept data and attach my monitor to these 2 functions. is there a way to connect to all signals connected to specific slot.
    thanks, (for reading if you have no answer

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to find all signals connected to specific slot

    Signals and slots are meant to be used when you don't care who registers for the signals or who sends the signal. If you require a tight coupling such as this, it might be better to implement a classic observer pattern. An alternative would be to use QSignalMapper to enumerate all signalling objects but then you'd lose your QByteArray argument. However you could implement something that works in a similar fashion. On the other hand you'd be practically implementing the observer pattern so it's probably easier to do it the right way in the beginning.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    davidovv (16th April 2012)

  4. #3
    Join Date
    Dec 2009
    Posts
    65
    Thanks
    10
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: how to find all signals connected to specific slot

    wysota, thanks for answering
    I hava a solution that can do the job: in the first line inside receive(QByteArray) slot i could emit another signal received(QByteArray), and to monitor (observe) communication i could connect to "send" and "received" signals, which (correct me if i am wrong) is the classic observer pattern. Somehow it seamed "dirty" to insert another signal when there are allready signals that do emit the same information, and i just need to find them and connect with them. I just wanted to monitor communication without modifying existing classes.
    Even if i have to do some for loops to find all signals connected to a slot, as oposed to connecting to a single "received" signal, i still think that the second way is "dirty" because i need to modify exixting class, which is complete, because of my new classes. And Signal/Slot idea is about not carring who is on the other side.
    So, I have a solution but i am searching for "correct" one.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to find all signals connected to specific slot

    I meant something like having a "register" method that feeds instances of a known interface to the class being observed that then calls methods from that interface when something interesting happens.

    Qt Code:
    1. class MonitorIface {
    2. public:
    3. virtual void onReceived(const QByteArray &ba) = 0;
    4. };
    5.  
    6. void XX::registerListener(MontorIface *iface) {
    7. m_listeners << iface;
    8. }
    9.  
    10. void XX::received() {
    11. QByteArray ba = ...;
    12. foreach(MonitorIface *iface, m_listeners) iface->onReceived(ba);
    13. }
    To copy to clipboard, switch view to plain text mode 

    Of course my example works "the other way round" but the principle is the same.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Dec 2009
    Posts
    65
    Thanks
    10
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: how to find all signals connected to specific slot

    wysota, thanks again, but
    my question is not just related to this example and it "evolves" to: is it possible to connect slot to slot, or in other words one slot to all signals of another slot?

    I know that i can count the number of receivers of a signal, but can i also connect to signals knowing only the slot?
    Imagine this "funny" scenario: you are at the restaurant, eating, i come after some time, and say to waiter give me the same thing he is eating, and i point to you. Is it possible to do that without looking into menu?
    Your suggestion in this scenario sounds like: i have to ask you to notify me when you order something so that i can order it too.
    I know you have a good taste in food but i dont want to disturb you, there is waiter that is paid for the job, and knows what you are eating

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to find all signals connected to specific slot

    There is no such thing as "signals of a slot", I have no idea what you mean. If you tell me what final result (in terms of functionality and not technical means to obtain it), maybe I'll be able to suggest a good solution.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Dec 2009
    Posts
    65
    Thanks
    10
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: how to find all signals connected to specific slot

    when i say "signals of a slot" i mean all signals connected to that slot.
    I thought that the restaurant example would help.
    Here is another pseudo code example
    Qt Code:
    1. class Person{
    2. signals:
    3. void speak(QString);
    4. public slots:
    5. void hear(QString)
    6. }
    7.  
    8. class Spy{
    9. public:
    10. void spyOnPerson(Person*)
    11. public slots:
    12. void personHeard(QString)
    13. void personSaid(QString)
    14. }
    15.  
    16.  
    17. //somewhere in application
    18. Person user1, user2, user3
    19. ...
    20. connect(&user1, speak(QString), &user2 hear(QString);
    21. connect(&user2, speak(QString), &user1 hear(QString);
    22. connect(&user3, speak(QString), &user1 hear(QString);
    23.  
    24. //now the spy comes to the scene
    25. Spy spy;
    26. ...
    27. spy.spyOnPerson(&user1);
    28. //user1 is some kind of criminal, and i wont to spy on him
    29. //i want spy to hear what user1 hears and what user1 says
    30. //in this exaple spy hears what user1 says, and what user2 and user3 say to user1
    31. //the problem is in assigning person to spy
    32. //i can say
    33. void Spy::spyOnPerson(Person *p){
    34. connect(p, speak(QString), this, personSaid(QString);
    35. //but i also need to hear what are the others speaking to user1
    36. connect(p, hear(QString), this personHeard(QString);
    37. //since hear is a slot i cant connect it to anything
    38. //but how can i search for other signals (user2 speak and user3 speak)
    39. //if i just know that hear is a slot and there are signals that call that slot
    40. //of course i can reemit another signal inside hear slot, and i did that
    41. //but it seamed like workaround to insert new signal that actualy executes when a signal (from another user) emits.
    42. //and signals are connectable to signals, and why should i create new signal, just search for the original signal and connect to it...
    43. }
    To copy to clipboard, switch view to plain text mode 

    Thinking more of this i start to understand that this is impossible (for now), I can't hear what i want without modifying Persons, i need to put a bug inside persons ears, and to connect to that... I just wanted to acomplish spying without modifying Person class.

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to find all signals connected to specific slot

    I don't want examples. I want you to tell me what you need the solution for.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #9
    Join Date
    Dec 2009
    Posts
    65
    Thanks
    10
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: how to find all signals connected to specific slot

    Ok, I have a lot of different devices (with different protocols) connected to a single serial port. Thread class that works with serial port emits signals when data is received, and has a slot for sending data. All devices have signals for sending and slots for receiving data. Device paired with port "exchanges" send and receive signals and slots. Because of the number of devices there is allways some activity on port, i want to monitor comunication for specific device, that is idle most of the time. I have a simple widget, created on demand, with log window that shows communication lines like
    timestamp <- "data recevied"
    timestamp -> "data sent"
    when i create this monitoring widget i connect it with serial port and show all communication
    too much communication makes it hard to catch the lines of this, almost idle, device
    i wanted to connect this widget with that specific device so that i can view only what is this device sending and receiving
    and i wanted my widget to be universal for monitoring any kind of communication
    As i said, i have a working solution, but i dont like that i have to edit classes for all devices, and to add a new "dataReceived" signal
    If it would be possible to "duplicate" connections of one specific receive slot, to another receive slot (of widget), that would solve my problem easy.
    Now i have to edit class files for all devices to implement this new behaviour, to reemit signal when data is received, for monitoring widget (if there is one attached).
    I just had a feeling that i could do this without editing the device class, and with having only the pointer of the device before showing the widget.
    I hope that this explanation didn't isnt unclear as previous ones.

    summary: i need to filter comunication from specific serial port, and tho show data for and from a specific device connected to it.
    Last edited by davidovv; 16th April 2012 at 17:40.

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to find all signals connected to specific slot

    If you ask me then what you need is a multiplexer/demultiplexer. When data is received on the port, something has to determine which device it is related to and needs to pass the data to the handler(s) for that device only. If you register your log widget as a handler for a particular device, it will also receive the data. You can implement it with signals or without them (e.g. by using QMetaObject::invokeMethod() or by ivoking a known interface as in my earlier example). I admit I don't see where the "who is connected to slot X" issue fits here
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #11
    Join Date
    Dec 2009
    Posts
    65
    Thanks
    10
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: how to find all signals connected to specific slot

    Multiplexing/demultiplexing idea is on my mind for some time, but too many devices, too many protocols. Anyway, every devices receive data for all devices, because they are on same serial port, and all devices have to be "resistant" on other devices data.
    Too many devices, too many protocols, and devices allready know how to handle "redudant" data, so demultiplexing is kind of redudant itself
    Quote Originally Posted by wysota View Post
    If you register your log widget as a handler for a particular device, it will also receive the data
    This sentence looks promising, can you point me to some examples, i have never done something simmilar.
    My widget should be created on user demand, and after the widget is closed, the device should continue to work like before widget.

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to find all signals connected to specific slot

    Quote Originally Posted by davidovv View Post
    Multiplexing/demultiplexing idea is on my mind for some time, but too many devices, too many protocols. Anyway, every devices receive data for all devices, because they are on same serial port, and all devices have to be "resistant" on other devices data.
    So how do you expect to show a log for only a single device?

    Too many devices, too many protocols, and devices allready know how to handle "redudant" data, so demultiplexing is kind of redudant itself
    Not really because if you have N devices, each message is processed N times instead of just one time.

    This sentence looks promising, can you point me to some examples, i have never done something simmilar.
    I'm talking about the observer pattern mentioned earlier.

    I think I don't really see what you're having problems with.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #13
    Join Date
    Dec 2009
    Posts
    65
    Thanks
    10
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: how to find all signals connected to specific slot

    So how do you expect to show a log for only a single device?
    (i knew that you will come with this hard question ) I want to see what a single device sends, and what that same single device receives. If it receives extra junk data, what can i do... i have to show that too, but sent data has to be only from that device.

    Not really because if you have N devices, each message is processed N times instead of just one time.
    I can live with that, because that is the real situation on serial port, every message is processed on every device. Actualy protocols have start sequence, end sequence, addresses checksums, so devices just ignore junk data that is not recognized as messages. If a real device can handle that, my class has to also, and processing it in every instance is not a problem, it is a "idiot proof" test of my protocol implementation also. I am still thinking of muliplexer/demultiplexer but i have no idea how to implement this new "protocol" layer.

    I'm talking about the observer pattern mentioned earlier.
    Observer pattern from wiki
    500px-Observer.svg.png
    if you look at notifyObserver as signal
    then it means that i have to emit a signal when i receive data, (i allready emit a signal when i send data)
    so the solution where i have to emit dataReceived signal in slot where i receive data kind of complies with observer pattern, and thank you for pointing me to that.
    I can emit this dataReceived signal only if the message is recognized, so monitoring can show only recognized messages

    I think I don't really see what you're having problems with.
    I have a solution for my problem, but i just wasn't sure that it is the "correct" solution.
    I thought that "correct" solution was to "hook" my slot on another slot if it is possible, but as i understand it isn't, SLOT DOESN'T EXIST
    Thanks for helping
    Last edited by davidovv; 16th April 2012 at 19:42.

  15. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to find all signals connected to specific slot

    Quote Originally Posted by davidovv View Post
    if you look at notifyObserver as signal
    Forget signals. Signals are good instead of an observer pattern, not as part of it.

    I think you are greatly complicating the problem where it doesn't need to be complicated.

    As for me you can implement your situation like this:

    Qt Code:
    1. struct CommunicationListener {
    2. virtual void onReceiveData(const QByteArray &ba) = 0;
    3. virtual void onSendData(Device *dev, const QByteArray &ba) = 0;
    4. };
    5.  
    6. void CommunicationController::registerListener(CommunicationListener *h) {
    7. m_listeners << h;
    8. }
    9.  
    10. /*!
    11.   slot for accepting data from devices
    12.  */
    13. void CommunicationController::sendData(const QByteArray &ba) {
    14. Device *dev = qobject_cast<Device*>(sender());
    15. foreach(CommunicationListener *l, m_listeners) l->onSendData(dev, ba);
    16. sendData_real(ba);
    17. }
    18.  
    19. /*!
    20.   method for receiving data from the port
    21.  */
    22. void CommunicationController::receiveData(const QByteArray &ba) {
    23. foreach(CommunicationListener *l, m_listeners) l->onReceiveData(ba);
    24. receiveData_real(ba);
    25. };
    26.  
    27. class LogWidget : public QListWidget, public CommunicationListener {
    28. public:
    29. // ...
    30. void onSendData(Device *dev, const QByteArray &ba) {
    31. if(dev != theOneIWant) return;
    32. QListWidgetItem *item = new QListWidgetItem(this);
    33. item->setText(ba);
    34. }
    35. void onReceiveData(const QByteArray &ba) {
    36. QListWidgetItem *item = new QListWidgetItem(this);
    37. item->setText(ba);
    38. }
    39. };
    To copy to clipboard, switch view to plain text mode 

    Of course if you are using threads, remember to make sure each method is called from the proper thread context.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #15
    Join Date
    Dec 2009
    Posts
    65
    Thanks
    10
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: how to find all signals connected to specific slot

    I was thinking "the other way round"
    First to create abstract class observableDevice,
    all devices should inherit from this one,
    the logwidget can take observableDevice as argument in constructor,
    and in constructor i can connect apropriate signals and slots

    This example is looks interesting.
    If i understand well, you suggest that i insert new layer (CommunicationControler) between devices and ports,
    devices now comunicate only with controllers, controllers are observable,
    log widget observes controller not device as i thought (with extra conditions if needed),
    and, what is important, no source file for device is changed, just their connections moved to controllers.

    I will definitly look more into this example, thanks

  17. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to find all signals connected to specific slot

    Quote Originally Posted by davidovv View Post
    This example is looks interesting.
    If i understand well, you suggest that i insert new layer (CommunicationControler) between devices and ports,
    devices now comunicate only with controllers, controllers are observable,
    log widget observes controller not device as i thought (with extra conditions if needed),
    and, what is important, no source file for device is changed, just their connections moved to controllers.
    It's hard to call it a layer, it's not above or between anything. It's more of a side block.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. #17
    Join Date
    Dec 2009
    Posts
    65
    Thanks
    10
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: how to find all signals connected to specific slot

    Quote Originally Posted by wysota View Post
    Forget signals. Signals are good instead of an observer pattern, not as part of it.
    Hi (wysota)
    After some time of "struggling" with new code i reached a dead end. I ended up with multiple virtual inheritance of QObject, and that is just not supported. So i made few steps back and now i am on the way to "forget signals" and to implement observer pattern (like you suggested the first time).
    I am using threads, and before i get a wrong idea how things work (again), i better ask how to:
    Quote Originally Posted by wysota View Post
    make sure each method is called from the proper thread context.
    a small example woul be appreciated

  19. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to find all signals connected to specific slot

    The simplest thing you can do is to do the callback via a custom event (posted to the object using QCoreApplication::postEvent()). Then Qt makes sure the method is called in the proper context.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 2
    Last Post: 26th August 2011, 08:51
  2. find connected network devices
    By Remco in forum Qt Programming
    Replies: 4
    Last Post: 9th September 2010, 09:03
  3. Connected QTimer slot not being called
    By Polnareff in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2010, 15:55
  4. Disconnect slot when another is being connected
    By holst in forum Qt Programming
    Replies: 4
    Last Post: 8th September 2009, 09:49
  5. Replies: 1
    Last Post: 6th March 2007, 15:27

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.