Get ComboBox text by table cell?



  • Hi guys.

    I have a table with 3 columns. When I press a plus button it creates a new row with the format: combobox, combobox, text.
    within the function that creates the new row I add the 2 combo boxes. The same variable is used on each pass so I was wondering how I can get the text of each combobox even though they are all using the same variable (they all are required to have the same contents). I have as follows:

    void Ifbox::on_ifplus_clicked()
    {
    QStringList list;

    int row = ui->ifbox_table->rowCount();
    int row2 = (row + 1);
    ui->ifbox_table->insertRow(row);
    QComboBox *comboWidget = new QComboBox;
    QComboBox *comboWidget2 = new QComboBox;
    if (!variablelist.isEmpty())
    comboWidget->addItems(variablelist);
    ui->ifbox_table->setCellWidget(row,0,comboWidget);
    ui->ifbox_table->setCellWidget(row,1,comboWidget2);
    comboWidget2->addItem("equals");
    comboWidget2->addItem("equals OR below");
    comboWidget2->addItem("equals OR over");
    comboWidget2->addItem("Is below");
    comboWidget2->addItem("Is over");}
    

    void Ifbox::on_saverule_button_clicked()
    {

    QString dbname = "ifbox";
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("ifbox.sqlite");
    QSqlQuery query(QSqlDatabase::database());
    
    int rowcount1 = ui->ifbox_table->rowCount();
    int rowcount2 = ui->thenbox_table->rowCount();
    QString profile = ui->profile_combobox->currentText();
    qDebug() << "rowcount: " << rowcount1 << rowcount2;
        if (rowcount1 > 0)
        {
        QString ifbox1 = ui->ifbox_table->item(0,0)->text();
        QString ifbox2 = ui->ifbox_table->item(0,1)->text();
        QString ifbox3 = ui->ifbox_table->item(0,2)->text();
        QSqlQuery query;
        query.prepare("UPDATE ifbox SET profile =:profile, result1=:ifbox1, comparator1=:ifbox2, state1=:ifbox3 WHERE ID=:page");
        query.bindValue(":ifbox1", ifbox1);
        query.bindValue(":ifbox2", ifbox2);
        query.bindValue(":ifbox3", ifbox3);
        query.bindValue(":profile", profile );
        query.bindValue(":page", page );
        query.exec();
        }
    

    The above section of a function was originally used to get text from the table but I have changed it to combo boxes.

    Any idea's guys?
    hope I have made my issue clear

    Thanks!


  • Lifetime Qt Champion

    Hi,

    This looks rather like a job for a QStyledItemDelegate and the use of a QSqlTableModel.



  • I did come across those functions and got kind of confused. I'm pretty new to QT and C++ in general.
    Is there any way I can use my current code. Would it be possible to perhaps create an array for each of the combo box rows so that all 30 variables can be accessed. ( The amount of max rows is 10 )


  • Lifetime Qt Champion

    You can get the widgets back from the cells, no need for an array.



  • My current code appears to crash while trying to get the text from the table cells inhabited by the combo boxes. It worked fine when it was only the table.

    if (rowcount1 > 0)
            {
            QString ifbox1 = ui->ifbox_table->item(0,0)->text();
            QString ifbox2 = ui->ifbox_table->item(0,1)->text();
            QString ifbox3 = ui->ifbox_table->item(0,2)->text();
            QSqlQuery query;
            query.prepare("UPDATE ifbox SET profile =:profile, result1=:ifbox1, comparator1=:ifbox2, state1=:ifbox3 WHERE ID=:page");
            query.bindValue(":ifbox1", ifbox1);
            query.bindValue(":ifbox2", ifbox2);
            query.bindValue(":ifbox3", ifbox3);
            query.bindValue(":profile", profile );
            query.bindValue(":page", page );
            query.exec();
            }
            if (rowcount1 > 1)
            {
            QSqlQuery query;
            QString ifbox4 = ui->ifbox_table->item(1,0)->text();
            QString ifbox5 = ui->ifbox_table->item(1,1)->text();
            QString ifbox6 = ui->ifbox_table->item(1,2)->text();
            query.prepare("UPDATE ifbox SET profile =:profile, result2 =:ifbox4, comparator2=:ifbox5, state2=:ifbox6 WHERE ID=:page");
            query.bindValue(":ifbox4", ifbox4);
            query.bindValue(":ifbox5", ifbox5);
            query.bindValue(":ifbox6", ifbox6);
            query.bindValue(":profile", profile );
            query.bindValue(":page", page );
            query.exec();
    

    etc etc etc.

    any idea why it is crashing?


  • Lifetime Qt Champion

    You're setting a widget over the items of your table and you try to get the data from the item underneath ? That won't work. The widget won't put anything there and if you didn't put any item at these position, you'll get a crash trying to access it.



  • @SGaist I assumed that you meant that I could get the text the same way using the table cells.
    So the only way to do this is via the functions you mentioned? I'm quite possibly out of my depth. I may just go back to the standard table and let the user type in the details.


  • Lifetime Qt Champion

    You really should take a look at the SpinBoxDelegate example Replace the QSpinBox by a QComboBox and there you have a nice editor for your choices.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.