我正在尝试的是在用户选择项目时获取QcomboBox的行号。虽然使用
很容易得到细胞柱和行cellClicked(int,int)
信号,但仅当单元格上没有小部件时才有效。
所以如果在单元格中放置了一个小部件,如何获取行号。
注意:所有组合框都是动态添加的
答案 0 :(得分:1)
最后我找到了两种方法。
第一种方法
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)));
function:rowFinder(int rowIndex)
int row = rowIndex; //here is the row indexof selected QComboBox