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. Get ComboBox text by table cell?
Qt 6.11 is out! See what's new in the release blog

Get ComboBox text by table cell?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 3.5k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    Scottish_Jason
    wrote on last edited by Scottish_Jason
    #1

    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!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Scottish_Jason
        wrote on last edited by
        #3

        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 )

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Scottish_Jason
            wrote on last edited by Scottish_Jason
            #5

            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?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              S 1 Reply Last reply
              0
              • SGaistS SGaist

                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.

                S Offline
                S Offline
                Scottish_Jason
                wrote on last edited by Scottish_Jason
                #7

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

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  • Login

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