Selecting QComboBox in QTableWidget

后端 未结 4 1952
梦谈多话
梦谈多话 2021-02-13 02:30

One cell in each row of a QTableWidget contains a combobox

for (each row in table ... ) {
   QComboBox* combo = new QComboBox();      
   table->setCellWidget         


        
4条回答
  •  孤独总比滥情好
    2021-02-13 02:56

    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.

提交回复
热议问题