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

how to write to QTableWidget Without ReWriting the previous row!



  • Hi ~~~
    I am a beginner i have QTableWidget i want to add some data on it so if the first row already have data i want it automatically to go to the second then the third row without Rewriting the previous rows ... etc ! any advice?

    QString ms1= QString::number(ui->priceofbuy_1->value());
    QString ms2 = QString::number(ui->numberofitemsspinbox_1->value());
    QString ms3 = QString::number(ui->firstpricelcd->value());
    
        
    ui->tableWidget->setItem(0, 0, new QTableWidgetItem(ui->nameofbuyercombobox_1->currentText()));
        ui->tableWidget->setItem(0, 1, new QTableWidgetItem(ui->comboBox_1->currentText()));
        ui->tableWidget->setItem(0, 2, new QTableWidgetItem(ms1));
        ui->tableWidget->setItem(0, 3, new QTableWidgetItem(ms2));
        ui->tableWidget->setItem(0, 4, new QTableWidgetItem(ms3));
    
        ui->tableWidget->insertRow(ui->tableWidget->rowCount());
    
        ui->nameofbuyercombobox_1->clear();
        ui->comboBox_1->clear();
        ui->priceofbuy_1->setValue(0.0);
        ui->numberofitemsspinbox_1->setValue(0.0);
        ui->firstpricelcd->display(0);
        ui->tableWidget->setRowCount(2);
    
        ui->tableWidget->setItem(1, 0, new QTableWidgetItem(ui->nameofbuyercombobox_1->currentText()));
        ui->tableWidget->setItem(1, 1, new QTableWidgetItem(ui->comboBox_1->currentText()));
        ui->tableWidget->setItem(1, 2, new QTableWidgetItem(ms1));
        ui->tableWidget->setItem(1, 3, new QTableWidgetItem(ms2));
        ui->tableWidget->setItem(1, 4, new QTableWidgetItem(ms3));
    


  • The following code may be helpful for you. This piece of code is adding a list of texts to the table.
    foreach( auto name, row_name_list ) {
    m_tableWidget->insertRow( m_tableWidget->rowCount() );
    auto item0 = new QTableWidgetItem( name );
    item0->setFont( font );
    item0->setTextAlignment( Qt::Align Left );
    m_tableWidget->setItem( m_tableWidget->rowCount() - 1, 0, item0 );
    auto item1 = new QTableWidgetItem( QString( "" ) );
    item1->setFont( font );
    item1->setTextAlignment( Qt::AlignLeft );
    m_tableWidget->setItem( m_tableWidget->rowCount() - 1, 1, item1 );
    }



  • works great bro <3
    Really Thank you i wish all the best happy life for you <3


Log in to reply