Selecting QComboBox in QTableWidget

后端 未结 4 1278
情书的邮戳
情书的邮戳 2021-02-13 02:10

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 03:03

    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.

提交回复
热议问题