单击时如何获取放置在Qtablewidget单元格中的小部件的行号?

How to get the row number of widget placed in a cell of Qtablewidget when it get clicked?

我正在尝试的是在用户选择项目时获取 QcomboBox 的行号。虽然使用

很容易获得单元格的列和行
cellClicked(int,int)

信号,但它仅在单元格上没有小部件时有效。

如果单元格中放置了小部件,那么如何获取行号。

注意:所有组合框都是动态添加的

最后我找到了两种方法。

  1. 通过设置QComboBox的属性
  2. 使用 QSignalMapper

第一种方法

QComboBox* mCombo = new QCombobox();
mComboBox->setProperty("row",(int) i); // i represents the row number in qtablewidget

在您处理点击的 QComboBox 的处理函数中

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

第二种方法

QSignalMapper *signalMapper= new QSignalMapper(this);   //Create a signal mapper instance 

for (each row in table) {
     QComboBox* mCombo = new QComboBox();
     table->setCellWidget(row,col,combo);                          
     connect(mCombo, SIGNAL(currentIndexChanged(int)), signalMapper, SLOT(map()));  

/*connect each signal of QComboBox to signal Mapper slot (i.e map()) which in turns connected to the signal of signalMapper calling the SLOT associated with it (i.e rowFinder) */         

signalMapper->setMapping(combo, (int)row);  //assign mapping to each widgetusing set mapping


}

connect(signalMapper, SIGNAL(mapped(int)),
         this, SLOT(rowFinder(int)));

函数:rowFinder(int rowIndex)

int row = rowIndex; //here is the row indexof selected QComboBox