1

I want to connect all signals of one QObject to a certain slot.

The slot looks something like this:

void SignalIntercepter::signalFired()
{
    std::cout << "Signal is fired!" << std::endl;
}

The following code is where the QObject will be passed to:

void SignalIntercepter::handleObject(QObject* object)
{
    const QMetaObject *me = object->metaObject();
    int methodCount = me->methodCount();
    for(int i = 0; i < methodCount; i++)
    {
        QMetaMethod method = me->method(i);
        if(method.methodType() == QMetaMethod::Signal)
        {
            // How do I connect this signal to the slot?
            // QObject::connect(object, ..., ..., ...);
        }
    }
}
Damnesia
  • 79
  • 1
  • 2
  • 8

1 Answers1

3

have a look at

const char * QMetaMethod::signature() const

then you should be able to use it like

QObject::connect(object, method->signature(), this, SLOT(signalFired()));

you might need to add "2" before the method->signature() call because SIGNAL(a) makro is defined SIGNAL(a) "2"#a as mentioned Is it possible to see definition of Q_SIGNALS, Q_SLOT, SLOT(), SIGNAL() macros? (Qt)

Community
  • 1
  • 1
Zaiborg
  • 2,492
  • 19
  • 28
  • Thank you! Without the "2" it gave me some problems. Also in Qt5.4 method.methodSignature() is used instead of signature(). I ended up using QObject::connect(object, "2" + method.methodSignature(), this, SLOT(signalFired())); – Damnesia Apr 24 '15 at 16:17