如何从插槽中找出哪个信号调用了该插槽? [英] How to find out from the slot which signal has called this slot?

查看:85
本文介绍了如何从插槽中找出哪个信号调用了该插槽?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思是,如果我有许多不同的信号连接到同一插槽.我看到了这个问题,但无法理解链接答案.能给我一个简单的例子吗?

I mean if I have many different signals which are connected to the same slot. I saw this question but can't understand the link in the answer. Can you give me simple example?

推荐答案

我认为您可以使用此方法: [protected] int QObject :: senderSignalIndex()const

I think you can use this method: [protected] int QObject::​senderSignalIndex() const

根据Qt文档:

返回调用当前正在执行的插槽的信号的元方法索引,该信号是sender()返回的类的成员.如果在信号激活的插槽之外调用,则返回-1.

Returns the meta-method index of the signal that called the currently executing slot, which is a member of the class returned by sender(). If called outside of a slot activated by a signal, -1 is returned.

对于具有默认参数的信号,此函数将始终返回带有所有参数的索引,无论与connect()一起使用哪个参数.例如,信号destroyed(QObject * obj = 0)将具有两个不同的索引(带有和不带有参数),但是此函数将始终返回带有参数的索引.当具有不同参数的信号过载时,这将不适用.

For signals with default parameters, this function will always return the index with all parameters, regardless of which was used with connect(). For example, the signal destroyed(QObject *obj = 0) will have two different indexes (with and without the parameter), but this function will always return the index with a parameter. This does not apply when overloading signals with different parameters.

警告:此功能违反了模块化的面向对象原理.但是,当多个信号连接到单个插槽时,访问信号索引可能会很有用.

Warning: This function violates the object-oriented principle of modularity. However, getting access to the signal index might be useful when many signals are connected to a single slot.

警告:从与该对象的线程不同的线程通过Qt :: DirectConnection调用插槽时,此函数的返回值无效.请勿在此类型的场景.

Warning: The return value of this function is not valid when the slot is called via a Qt::DirectConnection from a thread different from this object's thread. Do not use this function in this type of scenario.

此功能在Qt 4.8中引入.

This function was introduced in Qt 4.8.

这是我为您创建的一个小示例,展示了其工作原理:

Here is a small example that I created for you that demonstrates how it works:

#include <QTimer>
#include <QMetaObject>
#include <QMetaMethod>
#include <QCoreApplication>
#include <QDebug>
#include <QObject>

class Foo : public QObject
{
    Q_OBJECT
    public slots:
        void mySlot() {
            QMetaMethod metaMethod = sender()->metaObject()->method(senderSignalIndex());
            qDebug() << metaMethod.name();
            qDebug() << metaMethod.methodSignature();
            qApp->quit();
        }
};

#include "main.moc"

int main(int argc, char **argv)
{
    QCoreApplication coreApplication(argc, argv);
    QTimer timer;
    Foo foo;
    QObject::connect(&timer, &QTimer::timeout, &foo, &Foo::mySlot);
    timer.setSingleShot(true);
    timer.start(1000);
    return coreApplication.exec();
}

main.pro

TEMPLATE = app
TARGET = main
QT = core
CONFIG += c++11
SOURCES += main.cpp

构建并运行

qmake && make && ./main

输出

"timeout"
"timeout()"

这篇关于如何从插槽中找出哪个信号调用了该插槽?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆