Results 1 to 18 of 18

Thread: how to find all signals connected to specific slot

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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.

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
  •  
Qt is a trademark of The Qt Company.