0

Is it possible, given an object derived from qobject and a specific signal, to dynamically know the slot connected to that signal?

Guido Ranzuglia
  • 371
  • 2
  • 13
  • you mean all connections connected to that signal? – ratchet freak Sep 03 '13 at 13:20
  • 2
    No. The question is, why do you need to know? The point of signals/slots is that the connected objects need not be aware of each other. You can use http://qt-project.org/doc/qt-4.8/qobject.html#connectNotify if you just need to know if something is connected. For anything more than that, you will need to keep track of connections yourself, but you'd probably be better off modifying your design so you don't need to. – Dan Milburn Sep 03 '13 at 13:25
  • I have a QAction connected through the triggered() signal to a given slot. I have to pass this QAction to another part of my program. This other module has to create a new QAction (it cannot be the one I passed) but that must be connected to the same slot when the triggered signal is emitted. – Guido Ranzuglia Sep 03 '13 at 13:31

2 Answers2

0

You should take a look at QMetaObject::indexOfSignal, QMetaObject::indexOfSlot and QMetaObject::indexOfMethod.

QMetaObject* Meta = MyObject->metaObject();
int i = Meta->indexOfSlot("mySlot()");
if (i != -1) {
    // Has connection to slot
}

With QObject::receivers you returns the number of receivers connected to the signal.

if (MyObject->receivers(SIGNAL(mSignal()))) {
}
bkausbk
  • 2,740
  • 1
  • 36
  • 52
0

In the slot, you can use QObject::sender() method to get the object which emitted the signal. But i strongly recommend revising your design so you don't need to know the sender.

erelender
  • 6,175
  • 32
  • 49