假设我有两个信号(void signal1()
和 void signal2()
)和一个插槽(void slot()
) 。
两个信号都连接到插槽:
connect(this, &Classname::signal1, this, &Classname::slot);
connect(this, &Classname::signal2, this, &Classname::slot);
在slot()
的实现中,有没有办法知道哪个信号触发了这个槽?
最佳答案
您可以通过提供额外信息的 lambda...
connect(this, &Classname::signal1, this,
[source = 1, this]()
{
slot(source);
});
connect(this, &Classname::signal2, this,
[source = 2, this]()
{
slot(source);
});
(上面假设您当前的 Classname::slot
实现不带任何参数 - 进行调整以适应。)
关于c++ - 有没有办法知道Qt中哪个信号触发了插槽?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63487157/