Search code examples
c++qtqt4qt4.8

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


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


Solution

  • 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