} http://blog.csdn.net/koilin/article/details/7599223 我有两个QComboBox combo1, combo2:
- QComboBox *combo1 = new QComboBox;
- QComboBox *combo2 = new QComboBox;
- combo1->addItem("1-1");
- combo1->addItem("1-2");
- combo2->addItem("2-1");
- combo2->addItem("2-2");
- connect(combo1, SIGNAL(currentIndexChanged(int)), combo2, SLOT(process(int)));
- connect(combo2, SIGNAL(currentIndexChanged(int)), combo1, SLOT(process(int)));
QComboBox *combo1 = new QComboBox;
QComboBox *combo2 = new QComboBox;
combo1->addItem("1-1");
combo1->addItem("1-2");
combo2->addItem("2-1");
combo2->addItem("2-2");
connect(combo1, SIGNAL(currentIndexChanged(int)), combo2, SLOT(process(int)));
connect(combo2, SIGNAL(currentIndexChanged(int)), combo1, SLOT(process(int))); 然后是信号处理函数:
- void process(int index)
- {
- QComboBox *combo1 = new QComboBox;
- QComboBox *combo2 = new QComboBox;
- if (sender() == combo1)
- {
- combo2->setCurrentIndex(index);
- }
- else if (sender() == combo2)
- {
- combo1->setCurrentIndex(index);
- }
- }
void process(int index)
{
QComboBox *combo1 = new QComboBox;
QComboBox *combo2 = new QComboBox;
if (sender() == combo1)
{
combo2->setCurrentIndex(index);
}
else if (sender() == combo2)
{
combo1->setCurrentIndex(index);
}
} 这样两个combobox之间就产生了死循环。因为不管是手动改变combobox的当前index还是使用函数来设置当前的index都会发出index改变的消息,此时就需要使用关闭信号:
- void process(int index)
- {
- QComboBox *combo1 = new QComboBox;
- QComboBox *combo2 = new QComboBox;
- if (sender() == combo1)
- {
- combo2->blockSignals(true);
- combo2->setCurrentIndex(index);
- combo2->blockSignals(false);
- }
- else if (sender() == combo2)
- {
- combo1->blockSignals(true);
- combo1->setCurrentIndex(index);
- combo1->blockSignals(false);
- }
- }
|