How to get the row number of widget placed in a ce

2019-06-01 14:14发布

enter image description here

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

标签: c++ qt qt4 qt4.8
1条回答
Summer. ? 凉城
2楼-- · 2019-06-01 14:19

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
查看更多
登录 后发表回答