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

左心房为你撑大大i 提交于 2019-12-04 17:04:53

At-last i found 2 ways of doing it.

  1. By setting the property of QComboBox
  2. Using the QSignalMapper

First Method

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

In handler function where you are handling the clicked QComboBox

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

Second Method

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!