在QTableWidget中选择QComboBox [英] Selecting QComboBox in QTableWidget
问题描述
QTableWidget的每一行中的一个单元格包含一个组合框
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)));
....
}
在处理函数:: changed int index)我有
In the handler function ::changed(int index) I have
QComboBox* combo=(QComboBox*)table->cellWidget(_row,_col);
combo->currentIndex()
要获取组合框的副本
但是我不能获取行/ col。
当选择或更改嵌入项目时,不会发出表格单元格XXXX信号,并且currentRow )/ currentColumn()未设置。
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.
推荐答案
扩展Troubadour的 answer :
Expanding on Troubadour's answer:
这里是 QSignalMapper 文档以适应您的情况:
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 &)));
在处理程序函数:: changed(QString position):
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()
请注意,QString是一个相当笨拙的方法传递此信息。一个更好的选择是你传递一个新的QModelIndex,然后更改的函数将删除。
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.
这个解决方案的缺点是你失去currentIndexChanged发射的值,但您可以从:: changed中查询QComboBox的索引。
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.
这篇关于在QTableWidget中选择QComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!