#ifndef SIGNAL_WATCHER#define SIGNAL_WATCHER#include <QObject>class SignalWatcher : public QObject{ Q_OBJECTpublic: SignalWatcher( QObject* obj ); private slots: void dummy(); signals: void emitted(int);private: bool event( QEvent* e ); QObject* wObj; };#endif // SIGNAL_WATCHER
#include "sigwatcher.h"#include <QEvent>#include <QMetaMethod>#include <qobject_p.h> // hmmm... you can find it in $QTDIR/src/corelib/kernelSignalWatcher::SignalWatcher( QObject* obj ) : QObject(obj), wObj(obj){ const QMetaObject* mo = wObj->metaObject(); int n = mo->methodCount(); for ( int i = 0; i < n; i++ ) { QMetaMethod meth = mo->method(i); if ( meth.methodType() == QMetaMethod::Signal ) { QByteArray sign = "0"; sign[0] = sign[0] + QSIGNAL_CODE; sign += meth.signature(); connect( wObj, sign, this, SLOT(dummy()), Qt::QueuedConnection ); } }}bool SignalWatcher::event( QEvent* e ){ if ( e->type() == QEvent::MetaCall ) { QMetaCallEvent* me = (QMetaCallEvent*) e; int sigId = me->signalIdStart(); emit emitted(sigId); //debug const QMetaObject* mo = wObj->metaObject(); QMetaMethod meth = mo->method(sigId); qDebug("Signal %s emitted", meth.signature() ); //----- return true; } return false;}void SignalWatcher::dummy(){}
class SignalMapper : public QObject { Q_OBJECTpublic: SignalMapper(QObject *parent = 0):QObject(parent) {} void setMapping(QObject *object, const char *signal, int id);signals: void mapped(int id);private: QSignalMapper* freeMapper(QObject *object); QMultiMap<QObject*, QSignalMapper*> m_SignalMappers;};
void SignalMapper::setMapping(QObject *object, const char *signal, int id) { QSignalMapper *mapper = freeMapper(object); connect(object, signal, mapper, SLOT(map())); mapper->setMapping(object, id);}QSignalMapper* SignalMapper::freeMapper(QObject *object) { QList<QSignalMapper*> mappers = m_SignalMappers.values(); for (int i=0; i<mappers.size(); i++) { if (!m_SignalMappers.keys(mappers[i]).contains(object)) { m_SignalMappers.insert(object, mappers[i]); return mappers[i]; } } QSignalMapper *mapper = new QSignalMapper(this); m_SignalMappers.insert(object, mapper); connect(mapper, SIGNAL(mapped(int)), SIGNAL(mapped(int))); return mapper;}