QTableWidget的每行中的一个单元格包含一个组合框
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)中,我有
QComboBox* combo=(QComboBox*)table->cellWidget(_row,_col);
combo->currentIndex()以获取组合框的副本并获得新的选择。
但是我拿不到行/列。
当选择或更改嵌入项并且未设置currentRow()/currentColumn()时,不会发出任何表cellXXXX信号。
发布于 2014-10-02 23:13:27
不需要信号映射器...创建组合框后,您可以简单地向其添加两个自定义属性:
combo->setProperty("row", (int) nRow);
combo->setProperty("col", (int) nCol);在处理函数中,您可以将指针返回到信号的发送方(您的组合框)。
现在,通过请求属性,您可以恢复您的行/列:
int nRow = sender()->property("row").toInt();
int nCol = sender()->property("col").toInt();发布于 2009-08-26 14:00:58
扩展Troubadour的answer
以下是针对您的情况对QSignalMapper文档进行的修改:
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位置)中:
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,然后更改后的函数将删除该the。
这种解决方案的缺点是您会丢失currentIndexChanged发出的值,但是您可以从::changed中查询QComboBox的索引。
发布于 2009-08-26 05:34:58
我想你应该看看QSignalMapper。这听起来像是该类的典型用例,即您有一个对象集合,其中每个对象都挂接到相同的信号,但希望知道是哪个对象发出了该信号。
https://stackoverflow.com/questions/1332110
复制相似问题