绑定参数到信号/插槽 [英] Binding arguments to signals/slots

查看:141
本文介绍了绑定参数到信号/插槽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上有多个事件信号,我想连接到同一个插槽。我想知道的是如何将基于字符串的参数传递到同一个插槽,以便插槽知道这是来自哪个信号。一种替代方案是制作与信号一样多的时隙,然后以1:1的方式连接它们,但这是有效的,考虑到所有处理的代码非常相似。我尝试这样做,但我得到一些错误:

  connect(selecter1,SIGNAL(QString) SLOT(backgroundTypeChoiceMade(QString))); 
connect(button1,SIGNAL(clicked()),this,SLOT(backgroundTypeChoiceMade(button1)));
connect(button2,SIGNAL(clicked()),this,SLOT(backgroundTypeChoiceMade(button2)));

错误与我在最后两个命令中传递的参数有关。 c $ c> backgroundTypeChoiceMade 声明如下:

  void backgroundTypeChoiceMade(QString);有人可以告诉我上述代码中的错误是什么?


解决方案

您可以使用 QSignalMapper 。虽然QSignalMapper是你的问题的答案,我认为詹森·汉森的答案是你应该采取的方式。你会得到更清洁的代码。


I basically have multiple events signals which I want to connect to the same slot. What I want to know is how can I pass string based parameters to that same slot so that the slot knows which is this signal coming from. One alternative is to make as many slots as there are signals and then connect them in a 1:1 manner, but this is efficient, considering that the code for all the processing is very similar. I tried doing this but I'm getting some errors:

connect(selecter1,SIGNAL(selected(QString)),this,SLOT(backgroundTypeChoiceMade(QString)));
connect(button1,SIGNAL(clicked()),this,SLOT(backgroundTypeChoiceMade("button1")));
connect(button2,SIGNAL(clicked()),this,SLOT(backgroundTypeChoiceMade("button2")));

The error is related to the parameters I'm passing in the last 2 commands .. And backgroundTypeChoiceMade is declared like this:

void backgroundTypeChoiceMade(QString);

Can someone tell me what the error is in the above code ?

解决方案

You can use QSignalMapper. Although the QSignalMapper is the answer to your question, I think jon hanson's answer is the way you should take. You get much more cleaner code that way.

这篇关于绑定参数到信号/插槽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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