Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to save Qtablewidgte table to a row in sqlite
Qt 6.12 LTS is out! See what's new in the release blog

how to save Qtablewidgte table to a row in sqlite

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 2.2k Views 3 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi
    Well you just loop the row/cols and take data.
    https://forum.qt.io/topic/23764/save-qtablewidget/10

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #3

      @mrjj
      i use CellWidget "Qspinbox"
      and the function that you mentioned it is not working to get the values of the spinbox

      mrjjM 1 Reply Last reply
      0
      • ? A Former User

        @mrjj
        i use CellWidget "Qspinbox"
        and the function that you mentioned it is not working to get the values of the spinbox

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @davidlabib
        well you must get the text from the widget then.
        http://doc.qt.io/qt-5/qspinbox.html#cleanText-prop

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by A Former User
          #5

          @mrjj
          0_1537179032207_15a100fb-e501-4b27-bfc0-e880135f0e27-image.png
          i know how to get the value of the spinbox the probilm is there is alot of spinboxes
          0_1537179121641_7811cc68-23e2-4738-a0b5-98e92552d5e0-image.png
          in each row the user inserts it will make new spinbox

          E 1 Reply Last reply
          0
          • ? A Former User

            @mrjj
            0_1537179032207_15a100fb-e501-4b27-bfc0-e880135f0e27-image.png
            i know how to get the value of the spinbox the probilm is there is alot of spinboxes
            0_1537179121641_7811cc68-23e2-4738-a0b5-98e92552d5e0-image.png
            in each row the user inserts it will make new spinbox

            E Offline
            E Offline
            elfring
            wrote on last edited by
            #6

            i know how to get the value of the spinbox the probilm is there is alot of spinboxes

            • Would you like to reduce the number of used input control objects for your selected data set anyhow?
            • How do you think about restructure the shown source code example a bit more?
            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by A Former User
              #7

              @elfring

              void Invoices::on_barcodeIN_returnPressed()
              {
                  QSqlQuery qry;
                  qry.exec("SELECT NAME, UNITPRICE FROM DRUGS WHERE BARCODE='"+ui->barcodeIN->text()+"'");
                  //qry.exec("SELECT NAME, UNITPRICE FROM DRUGS WHERE\""+ui->barcodeIN->text()+"\"");
                  if(qry.next())
                  {
                      QString name = qry.value(0).toString();
                      QString unitprice = qry.value(1).toString();
                      float unitpriceint = qry.value(1).toFloat();
                      int x = ui->tableTW->rowCount();
                      ui->tableTW->setRowCount(x);
                      ui->tableTW->insertRow(x);
                      QLabel *drugNameLB = new QLabel(name);
                      QSpinBox *quantitySB = new QSpinBox();
                      QSpinBox *saleIN = new QSpinBox;
                      saleIN->setMinimum(0);
                      saleIN->setMaximum(100);
                      QDateEdit *expireDE = new QDateEdit();
                      QLabel *pharPriceLB = new QLabel(unitprice+" LE");
                      QLabel *pubpriceLB = new QLabel(unitprice+" LE");
                      float yy = unitpriceint - unitpriceint*(saleIN->value()/100);
                      pharPriceLB->setText(QString::number(yy) + " LE");
                      ui->tableTW->setCellWidget(x,0, drugNameLB);
                      ui->tableTW->setCellWidget(x,1, quantitySB);
                      ui->tableTW->setCellWidget(x,2, saleIN);
                      ui->tableTW->setCellWidget(x,3, expireDE);
                      ui->tableTW->setCellWidget(x,4, pharPriceLB);
                      ui->tableTW->setCellWidget(x,5, pubpriceLB);
                      QObject::connect(saleIN, SIGNAL(valueChanged(int)), this, SLOT(tee(int)));
                      ui->barcodeIN->clear();
                  }
              }
              

              what i want is to convert something like that
              0_1537182049250_24d7dbe5-cd5c-4758-91c2-22a4551b63c3-image.png
              to something like that
              0_1537182232816_c67404ad-97b7-46c6-b9d8-579fb8c04b5c-image.png
              saving the table in one row separated by ';'
              but the problim is that i use cellwidget
              i don't know how to get the value of spinbox in specific row

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

                Hi,

                QTableWidget::cellWidget is likely what you are looking for.

                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
                1
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #9

                  @SGaist

                  can you write me an example of how can i get the value of the spinbox in a variable

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

                    Something along the lines of:

                    QSpinBox *spinBox = qobject_cast<QSpinBox *>(myTableWidget->cellWidget(rowNumber, columnNumber);
                    if (!spinBox) {
                        qDebug() << "Wrong type of cell widget";
                        return;
                    }
                    int cellValue = spinBox->value();
                    

                    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
                    1
                    • ? A Former User

                      @elfring

                      void Invoices::on_barcodeIN_returnPressed()
                      {
                          QSqlQuery qry;
                          qry.exec("SELECT NAME, UNITPRICE FROM DRUGS WHERE BARCODE='"+ui->barcodeIN->text()+"'");
                          //qry.exec("SELECT NAME, UNITPRICE FROM DRUGS WHERE\""+ui->barcodeIN->text()+"\"");
                          if(qry.next())
                          {
                              QString name = qry.value(0).toString();
                              QString unitprice = qry.value(1).toString();
                              float unitpriceint = qry.value(1).toFloat();
                              int x = ui->tableTW->rowCount();
                              ui->tableTW->setRowCount(x);
                              ui->tableTW->insertRow(x);
                              QLabel *drugNameLB = new QLabel(name);
                              QSpinBox *quantitySB = new QSpinBox();
                              QSpinBox *saleIN = new QSpinBox;
                              saleIN->setMinimum(0);
                              saleIN->setMaximum(100);
                              QDateEdit *expireDE = new QDateEdit();
                              QLabel *pharPriceLB = new QLabel(unitprice+" LE");
                              QLabel *pubpriceLB = new QLabel(unitprice+" LE");
                              float yy = unitpriceint - unitpriceint*(saleIN->value()/100);
                              pharPriceLB->setText(QString::number(yy) + " LE");
                              ui->tableTW->setCellWidget(x,0, drugNameLB);
                              ui->tableTW->setCellWidget(x,1, quantitySB);
                              ui->tableTW->setCellWidget(x,2, saleIN);
                              ui->tableTW->setCellWidget(x,3, expireDE);
                              ui->tableTW->setCellWidget(x,4, pharPriceLB);
                              ui->tableTW->setCellWidget(x,5, pubpriceLB);
                              QObject::connect(saleIN, SIGNAL(valueChanged(int)), this, SLOT(tee(int)));
                              ui->barcodeIN->clear();
                          }
                      }
                      

                      what i want is to convert something like that
                      0_1537182049250_24d7dbe5-cd5c-4758-91c2-22a4551b63c3-image.png
                      to something like that
                      0_1537182232816_c67404ad-97b7-46c6-b9d8-579fb8c04b5c-image.png
                      saving the table in one row separated by ';'
                      but the problim is that i use cellwidget
                      i don't know how to get the value of spinbox in specific row

                      E Offline
                      E Offline
                      elfring
                      wrote on last edited by
                      #11

                      qry.exec("SELECT NAME, UNITPRICE FROM DRUGS WHERE BARCODE='"+ui->barcodeIN->text()+"'");

                      I suggest to use a prepared query instead of constructing the selection in a questionable way.

                      ui->tableTW->setRowCount(x);

                      • I guess that it will help to use a reference variable for the relevant table.
                      • How do you think about to work with a specific class for the records?
                      • Could it be that the use of the class “QTableView” would be more appropriate than “QTableWidget” (together with a customised data model)?

                      saving the table in one row separated by ';'

                      • Do you really need to concatenate selected values into an enumeration for each field?
                      • Can it be nicer to keep the filtered records separate?
                      1 Reply Last reply
                      2

                      • Login

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