Important: Please read the 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

    Well you just loop the row/cols and take data.

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

    well you must get the text from the widget then.

  • @mrjj
    i know how to get the value of the spinbox the probilm is there is alot of spinboxes
    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()+"\"");
            QString name = qry.value(0).toString();
            QString unitprice = qry.value(1).toString();
            float unitpriceint = qry.value(1).toFloat();
            int x = ui->tableTW->rowCount();
            QLabel *drugNameLB = new QLabel(name);
            QSpinBox *quantitySB = new QSpinBox();
            QSpinBox *saleIN = new QSpinBox;
            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)));

    what i want is to convert something like that
    to something like that
    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


    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";
    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.


    • 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