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

how to save Qtablewidgte table to a row in sqlite



  • i have Qtablewidgte inserted by the user and i want to save it in a Sqlite row
    separated by ';'


  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

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



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



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


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


  • Lifetime Qt Champion

    Hi,

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



  • @SGaist

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


  • Lifetime Qt Champion

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


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

Log in to reply