qtqtablewidgetqstringqlineeditqspinbox

How to read information of QTableWidget?


I have a QtableWidget and I have stored a QSpinBox in. as below: (product is a class).

void MainWindow:: add_to_basket (product p){

    ui->tableWidget->insertRow(0);

    QLineEdit *qle=new QLineEdit();
    qle->setText(p.get_name());
    ui->tableWidget->setCellWidget(0,0,qle);

    QLineEdit *qle1=new QLineEdit();
    qle1->setText(QString::number(p.get_price()));
    ui->tableWidget->setCellWidget(0,1,qle1);

    QSpinBox *qsb=new QSpinBox();
    qsb->setValue(p.get_count());
    ui->tableWidget->setCellWidget(0,2,qsb);
}

now I want to access data stored in QSpinBox but don't know how?


Solution

  • First, inserting to row 0 of the QTableWidget every time will likely be a problem, you may want to check that as well.

    Answering your question now, to get the value from the spinbox, simply use qsb->value()

    See: https://doc.qt.io/qt-5/qspinbox.html