本文介绍了QSignalMapper如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的帖子之后:我需要关联:

After my post here : Associate signal and slot to a qcheckbox create dynamically I need to associate :

•信号 clicked()当我点击 qCheckBox 到函数 cliqueCheckBox(QTableWidget * monTab,int ligne,QCheckBox * pCheckBox)

• The signal clicked() when I click on a qCheckBox to my function cliqueCheckBox(QTableWidget *monTab, int ligne, QCheckBox *pCheckBox)

要这样做,我必须使用 QSignalMapper ,经过两个小时的尝试来了解其工作原理后,我可以t效果很好,这是我编写的代码,这显然是错误的:

To do so, I have to use QSignalMapper, after two hours of trying to understand how it works, I can't have a good result, here's the code I make, this is obviously wrong :

 QSignalMapper *m_sigmapper = new QSignalMapper(this);
 QObject::connect(pCheckBox, SIGNAL(mapped(QTableWidget*,int, QCheckBox*)), pCheckBox, SIGNAL(clicked()));
 QObject::connect(this, SIGNAL(clicked()), this, SLOT(cliqueCheckBox(QTableWidget *monTab, int ligne, QCheckBox *pCheckBox)));

 m_sigmapper->setMapping(pCheckBox, (monTab,ligne, pCheckBox));
 QObject::connect(m_sigmapper, SIGNAL(clicked()),this, SLOT(cliqueCheckBox(QTableWidget *monTab, int ligne, QCheckBox *pCheckBox)));

您能跟我解释一下 QSignalMapper 有用吗?我不太了解要与什么关联:(

Can you explain to me, how QSignalMapper works ? I don't really understand what to associate with :(

推荐答案

QSignalMapper 类收集一组无参数的信号,并使用与发送该信号的对象相对应的整数,字符串或窗口小部件参数重新发出它们。因此,您可以像这样:

QSignalMapper class collects a set of parameterless signals, and re-emits them with integer, string or widget parameters corresponding to the object that sent the signal. So you can have one like:

QSignalMapper * mapper = new QSignalMapper(this);
QObject::connect(mapper,SIGNAL(mapped(QWidget *)),this,SLOT(mySlot(QWidget *)));

对于每个按钮,您都可以连接 clicked() 信号到 QSignalMapper map()插槽,并使用setMapping添加映射,以便当从按钮发出 clicked()信号时,发出 mapped(QWidget *)信号:

For each of your buttons you can connect the clicked() signal to the map() slot of QSignalMapper and add a mapping using setMapping so that when clicked() is signaled from a button, the signal mapped(QWidget *) is emitted:

QPushButton * but = new QPushButton(this);

QObject::connect(but, SIGNAL(clicked()),mapper,SLOT(map()));
mapper->setMapping(but, but);

这样,每当您单击按钮时,都会发出映射器的 mapped(QWidget *)信号,其中包含窗口小部件作为参数。

This way whenever you click a button, the mapped(QWidget *) signal of the mapper is emitted containing the widget as a parameter.

这篇关于QSignalMapper如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-08 17:27