Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Update qCombobox inside qtableWidget with sqlite data



  • Hi, I have a problem with my current code, I have inserted in qTableWidget a combobox with with setCellWidget method, but I need to update the value in that qComboBox as I do with ui->tW_Datos->setItem(fila, 0, new QTableWidgetItem(consultar.value(2).toByteArray().constData())) for example, how Can I update my combobox with the value from sq|lite??

    void Complejidad::MostrarDatos()
    {
    QString consulta;
    consulta.append("SELECT * FROM componentes WHERE Componente='" + buscar + "'");
    QSqlQuery consultar;
    consultar.prepare(consulta);

    if(consultar.exec()){
        qDebug() << "Se ha consultado correctamente";
    }else{
        qDebug() << "No se ha consultado correctamente" << consultar.lastError();
    }
    
    int fila=0;
    
    ui->tW_Datos->setRowCount(0);
    
    while(consultar.next()){
        QComboBox *combo;
        combo = new QComboBox;
        combo->addItems({"", "Simple", "Media", "Alta"});
        combo->setProperty("row", 1 - ui->tW_Datos->rowCount());
        combo->setProperty("column", 1);
    
        ui->tW_Datos->insertRow(fila);
        ui->tW_Datos->setItem(fila, 0, new QTableWidgetItem(consultar.value(2).toByteArray().constData()));
        connect(combo, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(OnComboIndexChanged(const QString&)));
        ui->tW_Datos->setCellWidget (fila, 1, combo);
    }
    

    }



  • @Isidro-Perla
    I'm not sure what your issue is here.

    You can retrieve the QComboBox created via QTableWidget::setCellWidget(row, column, combo) via combo = qobject_cast<QComboBox*>QTableWidget::cellWidget(row, column). Is that what you are looking for?



  • Well, I have a problem with this line:

    combo = qobject_cast<QComboBox*>QTableWidget:cellWidget(row, column);

    return this error: assigning to 'QComboBox *' from incompatible type '<overloaded function type>'

    How Can I fix that?



  • @Isidro-Perla
    You must put your variable in place of the QTableWidget in QTableWidget::cellWidget(), so
    combo = qobject_cast<QComboBox*>yourTableWidget->cellWidget(row, column);

    I see you have ui->tW_Datos->setCellWidget (fila, 1, combo);, you to get it back:
    combo = qobject_cast<QComboBox*>ui->tW_Datos->cellWidget(fila, 1);


Log in to reply