如何将 sender() 名称转换为 int

How to convert sender() name into an int

我有一个程序,其中创建了多个组合框,这个数字相当于一个数组的长度。我需要确保当信号 currentIndexChanged 发送到我的插槽时,插槽知道组合框的索引以及发送它的组合框。第一个组合框的索引需要写入数组中的第一个元素。为此,插槽需要知道哪个组合框发送了信号。我尝试通过 QSignalMapper 完成此操作,但它只能发送一个参数。我也尝试使用 sender() 函数,但 return 是一个对象而不是对象的编号。我有什么办法可以做到这一点吗?

    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)));

   }

创建小部件时,执行

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

(根据喜好选择 属性 名称;实际的字符串无关紧要)。

收到信号后,

int x = sender()->property("MyIndex").toInt();