tags:

views:

1476

answers:

3

One cell in each row of a QTableWidget contains a combobox

for (each row in table ... ) {
   QComboBox* combo = new QComboBox();      
   table->setCellWidget(row,col,combo);    
   combo->setCurrentIndex(node.type());     
   connect(combo, SIGNAL(currentIndexChanged(int)),this, SLOT(changed(int)));
   ....
}

In the handler function ::changed(int index) I have

QComboBox* combo=(QComboBox*)table->cellWidget(_row,_col);  
combo->currentIndex()

To get back a copy of the combobox and get the new selection.
But I can't get the row/col.
None of the table cellXXXX signals is emitted when an embedded item is selected or changed and currentRow()/currentColumn() aren't set.

+1  A: 

I think you want to take a look at QSignalMapper. This sounds like a typical use case for that class i.e. you have a collection of objects where you hook up to the same signal on each but would like to know which object emitted the signal.

Troubadour
+2  A: 

Expanding on Troubadour's answer:

Here's a modification of the QSignalMapper documentation to fit your situation:

 QSignalMapper* signalMapper = new QSignalMapper(this);

 for (each row in table) {
     QComboBox* combo = new QComboBox();
     table->setCellWidget(row,col,combo);                         
     combo->setCurrentIndex(node.type()); 
     connect(combo, SIGNAL(currentIndexChanged(int)), signalMapper, SLOT(map()));
     signalMapper->setMapping(combo, QString("%1-%2).arg(row).arg(col));
 }

 connect(signalMapper, SIGNAL(mapped(const QString &)),
         this, SIGNAL(changed(const QString &)));

In the handler function ::changed(QString position):

 QStringList coordinates = position.split("-");
 int row = coordinates[0].toInt();
 int col = coordinates[1].toInt();
 QComboBox* combo=(QComboBox*)table->cellWidget(row, col);  
 combo->currentIndex()

Note that a QString is a pretty clumsy way to pass this information. A better choice would be a new QModelIndex that you pass, and which the changed function would then delete.

The downside to this solution is that you lose the value that currentIndexChanged emits, but you can query the QComboBox for its index from ::changed.

Bill
connect(signalMapper, SIGNAL(mapped(const QString
Martin Beckett
A: 

@Bill - Shouldn't that be:

connect(signalMapper, SIGNAL(mapped(const QString &)),this, SLOT(changed(const QString &)));

Doesn't format correctly as a comment - sorry,

Martin Beckett
Hmm, it's been so long, I don't remember if that was intentional or not. (I do sometimes hook signals straight up to other signals.) I'm using a QSignalMapper at the moment, so I'll compare what actually works to my answer, thanks!
Bill