且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在QTableWidget中选择QComboBox

更新时间:2023-11-13 12:39:22

扩展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.