简体   繁体   English

动态映射一个对象的所有QT信号到一个插槽

[英]Dynamic mapping of all QT signals of one object to one slot

I want to connect all signals of one QObject to a certain slot. 我想将一个QObject的所有信号连接到某个槽。

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: 以下代码是将QObject传递给的位置:

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, ..., ..., ...);
        }
    }
}

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? 您可能需要在method->signature()调用之前添加"2" ,因为SIGNAL(a) makro定义为SIGNAL(a) "2"#a如上所述是否可以看到Q_SIGNALS,Q_SLOT,SLOT()的定义,SIGNAL()宏? (Qt) (QT)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM