我有一个接收我创建的类的 QList 的函数。我们称这个假设的类为“Stuff”。所以,这个函数接收一个 QList of Stuff
我遍历 QList,根据“Stuff”对象的属性,我将生成以下内容之一:
1) QLine编辑
2) QCheckBox
3) QComboBox
这不是实际的代码,但这就是我本质上在做的事情:
void MyProgram::update(QList<Stuff> myStuffs)
{
this->mSignalMapper = new QSignalMapper();
foreach (Stuff stuff, myStuffs)
{
if (stuff.isInt())
{
QLineEdit* input = new QLineEdit();
//There is code here to setup the QLineEdit and fill an initial value
verticalLayout->addWidget(input); //verticalLayout is QVBoxLayout
QObject::connect(input, SIGNAL(editingFinished()), this->mSignalMapper, SLOT(map()));
this->mSignalMapper->setMapping(input, stuff.getMappingId());
/*
* NOTE: the stuff.getMappingId() function returns an int that is unique
* to that stuff object. I'm 100% sure each stuff object is getting
* a unique mapping ID */
QObject::connect(this->mSignalMapper, SIGNAL(mapped(int)), this, SLOT(onStuffChanged(int)));
}
else if (stuff.isBool())
{
QCheckBox* input = new QCheckBox();
//There is code here to setup the QCheckBox and set an initial value
verticalLayout->addWidget(input);
QObject::connect(input, SIGNAL(stateChanged(int)), this->mSignalMapper, SLOT(map()));
this->mSignalMapper->setMapping(input, stuff.getMappingId());
QObject::connect(this->mSignalMapper, SIGNAL(mapped(int)), this, SLOT(onStuffChanged(int)));
}
else if (stuff.isStringList())
{
QComboBox* input = new QComboBox();
//There is code here to setup the QComboBox and fill in values for the combo box
verticalLayout->addWidget(input);
QObject::connect(input, SIGNAL(activated(int)), this->mSignalMapper, SLOT(map()));
this->mSignalMapper->setMapping(input, stuff.getMappingId());
QObject::connect(this->mSignalMapper, SIGNAL(mapped(int)), this, SLOT(onStuffChanged(int)));
}
}
}
问题是,如果我通过编辑 QLineEdit、选中复选框或更改组合框值仅触发 Widget 的一个信号,则会调用 onStuffChanged(int) 函数 N 次,其中 N = number of mSignalMapper 的映射。
这里发生了什么?如果我遍历并创建 10 个小部件,则仅单击 10 个小部件中的 1 个会调用该函数 10 次,而这 10 次中的每一次都会传递与仅与我交互的 1 个对象相关联的唯一 int。因此,如果 10 个小部件中的第一个是唯一 int ID 为 27 的复选框,则 onStuffChanged(int) 函数将被调用 10 次,每次参数为 27。