Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Update qCombobox inside qtableWidget with sqlite data
Qt 6.11 is out! See what's new in the release blog

Update qCombobox inside qtableWidget with sqlite data

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 496 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Isidro Perla
    wrote on last edited by
    #1

    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);
    }
    

    }

    JonBJ 1 Reply Last reply
    0
    • I Isidro Perla

      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);
      }
      

      }

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @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?

      1 Reply Last reply
      1
      • I Offline
        I Offline
        Isidro Perla
        wrote on last edited by
        #3

        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?

        JonBJ 1 Reply Last reply
        0
        • I Isidro Perla

          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?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @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);

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved