2

I have a program in which a number of combo boxes are created and this number is equivalent to the length of an array. I need to make sure that when the signal currentIndexChanged is sent to my slot, the slot knows the index of the combo box and which combo box sent it. The index of the first combo box needs to written to the first element in the array. To do this the slot needs to know which combo box sent the signal. I tried accomplishing this through QSignalMapper but that can only send one parameter. I also tried using the sender() function but the return is an object not the number of the object. Is there any way I can accomplish this?

    int lenght = sizeof(countries)/sizeof(countries[0]);

        for(int x=0; x<=lenght-1; x++)
  {

            QComboBox* combo = new QComboBox;
            combo->addItem("Present");
            combo->addItem("Present and Voting");
            combo->addItem("Absent");
            combo->addItem("Absent from Commitee");
 formLayout->addRow(countries[x],combo);
 connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(roll(int)));

   }
Abhi Garg
  • 73
  • 7
  • why not just pass a pointer to a combo box to the slot, from the signal (just have both the signal and the slot function take a `QComboBox *` parameter – user Mar 02 '18 at 23:17

1 Answers1

2

When creating the widgets, do

combo->setProperty("MyIndex", x);

(choose property name to taste; the actual string doesn't matter).

When receiving the signal, do

int x = sender()->property("MyIndex").toInt();
Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85