15 c++ qt qt4 qtablewidget qcombobox
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)));
....
}
Run Code Online (Sandbox Code Playgroud)
在处理程序函数:: changed(int index)中我有
QComboBox* combo=(QComboBox*)table->cellWidget(_row,_col);
combo->currentIndex()
Run Code Online (Sandbox Code Playgroud)
要获取组合框的副本并获得新选择.
但我无法获得行/列.
当选择或更改嵌入项并且未设置currentRow()/ currentColumn()时,不会发出表cellXXXX信号.
Ric*_*chy 11
无需信号映射器...创建组合框后,您只需向其添加两个自定义属性:
combo->setProperty("row", (int) nRow);
combo->setProperty("col", (int) nCol);
Run Code Online (Sandbox Code Playgroud)
在处理函数中,您可以获得指向信号发送者(您的组合框)的指针.
现在通过询问属性,您可以返回行/列:
int nRow = sender()->property("row").toInt();
int nCol = sender()->property("col").toInt();
Run Code Online (Sandbox Code Playgroud)
扩展Troubadour的答案:
以下是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 &)));
Run Code Online (Sandbox Code Playgroud)
在处理函数:: 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()
Run Code Online (Sandbox Code Playgroud)
请注意,QString是传递此信息的一种非常笨拙的方式.更好的选择是您传递的新QModelIndex,然后更改的函数将被删除.
此解决方案的缺点是您丢失了currentIndexChanged发出的值,但您可以从:: changed查询QComboBox的索引.