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

南楼画角 提交于 2019-12-21 21:34:58

问题


What i'm trying is to get the row number of QcomboBox when user selects items. Although its easy to to get the cell column and row using

cellClicked(int,int)

signal, but it only works when there is no widget on the cell.

so how to get the row number in case if there is a widget placed in a cell.

Note: All the combobox are added dynamically


回答1:


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


来源:https://stackoverflow.com/questions/45182348/how-to-get-the-row-number-of-widget-placed-in-a-cell-of-qtablewidget-when-it-get

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