Results 1 to 12 of 12

Thread: Querying signal/slot state at runtime

  1. #1
    Join Date
    Jan 2011
    Posts
    32
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Querying signal/slot state at runtime

    Is there a way to query the state of the signal/slot connections at runtime? i.e. if I am an instantiated instance of class A (inherited from QObject, of course), can I go look somewhere to see which objects are currently connected to my signal B()? I am looking at QMetaObject, but it only seems to list the signals and slots that I have available, not what their current state of connection is.

  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: Querying signal/slot state at runtime

    There is no way to do that. You can only use QObject::connectNotify() and QObject::disconnectNotify().
    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:

    Ashkan_s (12th October 2012)

  4. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Querying signal/slot state at runtime

    If some type of signal generation is time intensive and you only want to do the work and produce the signal if something is connected to it, then you can also use QObject::receivers() as far as I'm aware rather than keeping a count yourself.

  5. The following user says thank you to squidge for this useful post:

    Ashkan_s (12th October 2012)

  6. #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: Querying signal/slot state at runtime

    The problem with the receivers() method is that it is probably unreliable in the sense that its result is not trustworthy. You can query for receivers and immediately after this method returns a connection can be made to the object in another thread before any more code has a chance to run.
    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.


  7. #5
    Join Date
    Jan 2011
    Posts
    32
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Querying signal/slot state at runtime

    squidge:

    Yes, that does look like it will accomplish what I am looking for. Basically I just need to query if a particular signal has anyone connected to it at all at a particular time. I'm not interested in an itemized object list.

    wysota:

    I am sitting here right now experimenting with connectNotify and disconnectNotify. The connectNotify events are coming through nicely. The disconnectNotify, however, does not seem to work as I expect. When I connect object B to object A and then destroy object B, object A is not receiving a disconnectNotify. I also tried an explicit disconnect(SIGNAL) call in object B just before destroying it, and object A is still not receiving a disconnectNotify.

    Any working examples available to help me see if I am doing something wrong?

  8. #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: Querying signal/slot state at runtime

    disconnectNotify() will not be called if an object is destroyed without prior disconnection. Unfortunately receivers() will not help you in some cases either. What exactly do you need this functionality 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.


  9. #7
    Join Date
    Jan 2011
    Posts
    32
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Querying signal/slot state at runtime

    I am writing a graphics drawing program, and I want to be able to dynamically "parent" objects to one another. Parenting in my case involves connecting a particular signal on the "parent" to a slot on the "child," not using the setParent() method as might seem more intuitive. I wish to set the constraint that if an object is already a "child", that object cannot have two "parent"s (i.e. there can't be two "parents" hooked to the "child"s slot). As well, if an object is already a "child," that object is then not allowed to "parent" other objects.

    I could set up some sort of manager class to track all of this with my dynamically created/destroyed objects, but as I am only connecting one slot to one signal to accomplish my "parenting," it seems more direct to me to be able to just query the current signal/slot connection state on that particular signal/slot.

  10. #8
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Querying signal/slot state at runtime

    Hi,

    I don't think you need to go that far. If setParent() does not work for you. You can also have a public member QGraphicsItem *myparent started in 0; You can dynamically set myparent to the parent. If parent is not assigned means that the object has no parent. With this you can control that a child cannot have two parents.

    In terms of signals if you parent the object you can connect a signal from the parent to the child and viceversa.

  11. #9
    Join Date
    Jan 2011
    Posts
    32
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Querying signal/slot state at runtime

    Yes, qlands, that may fit. I didn't understand one thing though.

    You can also have a public member QGraphicsItem *myparent started in 0
    What do you mean by "started in 0"?

  12. #10
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Querying signal/slot state at runtime

    I mean that you can have a public member in your child
    Qt Code:
    1. public:
    2. MyParentClass *myparent; //In this case I'm asuming MyParentClass is a subclass of any QGraphicsItem
    To copy to clipboard, switch view to plain text mode 

    and in the constructor of the child you have:

    Qt Code:
    1. myparent = 0;
    To copy to clipboard, switch view to plain text mode 

    then you can check when parenting the child:
    Qt Code:
    1. if (!myparent) //Only if my parent is 0 which mean that it does not have a parent
    2. {
    3. myparent = mynewparent;
    4. }
    To copy to clipboard, switch view to plain text mode 

  13. The following user says thank you to qlands for this useful post:

    lxman (8th March 2011)

  14. #11
    Join Date
    Jan 2011
    Posts
    32
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Querying signal/slot state at runtime

    Oh, okay, I understand now. I think this idea is the basic one I am going to go with. The actual implementation is going to be quite a bit more complex, but the direction looks good to me.

    Thanks to all for giving me the suggestions.

  15. #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: Querying signal/slot state at runtime

    Quote Originally Posted by lxman View Post
    I am writing a graphics drawing program, and I want to be able to dynamically "parent" objects to one another. Parenting in my case involves connecting a particular signal on the "parent" to a slot on the "child," not using the setParent() method as might seem more intuitive. I wish to set the constraint that if an object is already a "child", that object cannot have two "parent"s (i.e. there can't be two "parents" hooked to the "child"s slot). As well, if an object is already a "child," that object is then not allowed to "parent" other objects.

    I could set up some sort of manager class to track all of this with my dynamically created/destroyed objects, but as I am only connecting one slot to one signal to accomplish my "parenting," it seems more direct to me to be able to just query the current signal/slot connection state on that particular signal/slot.
    You don't need signals in this case. You can call the method directly as the child and the parent know about each other:
    Qt Code:
    1. void Parent::someMethod() {
    2. // ...
    3. foreach(Child *chld, m_children) chld->someOtherMethod();
    4. // ...
    5. }
    To copy to clipboard, switch view to plain text mode 

    Using signals makes sense when objects do not know about each other's existence.
    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: 0
    Last Post: 19th December 2010, 16:03
  2. Replies: 1
    Last Post: 7th August 2010, 22:38
  3. Replies: 0
    Last Post: 24th May 2010, 08:11
  4. add signal/slots at runtime?
    By oc2k1 in forum Qt Programming
    Replies: 10
    Last Post: 15th June 2009, 19:52
  5. Replies: 5
    Last Post: 9th May 2007, 21:31

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.