One cell in each row of a QTableWidget contains a combobox
for (each row in table ... ) {
QComboBox* combo = new QComboBox();
table->setCellWidget
Just got same problem and this is how I solved. I use QPoint that is a cleaner way to save a x-y value than a QString. Hope this helps.
classConstructor() {
//some cool stuffs here
tableVariationItemsSignalMapper = new QSignalMapper(this);
}
void ToolboxFrameClient::myRowAdder(QString price) {
QLineEdit *lePrice;
int index;
//
index = ui->table->rowCount();
ui->table->insertRow(index);
//
lePrice = new QLineEdit(price);
connect(lePrice, SIGNAL(editingFinished()), tableVariationItemsSignalMapper, SLOT(map()));
tableVariationItemsSignalMapper->setMapping(lePrice, (QObject*)(new QPoint(0, index)));
// final connector to various functions able to catch map
connect(tableVariationItemsSignalMapper, SIGNAL(mapped(QObject*)),
this, SLOT(on_tableVariationCellChanged(QObject*)));
}
void ToolboxFrameClient::on_tableVariationCellChanged(QObject* coords) {
QPoint *cellPosition;
//
cellPosition = (QPoint*)coords;
}
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.
No need for the signal mapper... When the combobox is created you can simply add two custom properties to it:
combo->setProperty("row", (int) nRow);
combo->setProperty("col", (int) nCol);
In the handler function you can get a pointer back to the sender of the signal (your combobox).
Now by asking for the properties you can have your row/col back:
int nRow = sender()->property("row").toInt();
int nCol = sender()->property("col").toInt();
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, SLOT(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.