Planned maintenance has been done but it did not solve the problem. So work will continue on this and a new time for trying updates will be announced asap.

concatanating qtablewidget items into string incorrect

  • In my qt c++ application I have created a tablewidget with 2 coulmns and 5 rows and added items as follows!


    ui->tableWidget->setItem(0,0,new QTableWidgetItem("John"));
    ui->tableWidget->setItem(0,1,new QTableWidgetItem("smith"));
    ui->tableWidget->setItem(1,0,new QTableWidgetItem("Jimmy"));
    ui->tableWidget->setItem(1,1,new QTableWidgetItem("michel"));
    ui->tableWidget->setItem(2,0,new QTableWidgetItem("Russel"));
    ui->tableWidget->setItem(2,1,new QTableWidgetItem("Arnold"));
    ui->tableWidget->setItem(3,0,new QTableWidgetItem("Mary"));
    ui->tableWidget->setItem(3,1,new QTableWidgetItem("Anne"));
    ui->tableWidget->setItem(4,0,new QTableWidgetItem("shaun"));
    ui->tableWidget->setItem(4,1,new QTableWidgetItem("Paul"));

    I want to concatanate these names in to a single string and display on a label!
    void MainWindow::on_pushButton_2_clicked()
    for(int i=0;i<5;i++){
    QStringList concat;
    QTableWidgetItem *m=ui->tableWidget->item(i,0);
    concat<< m->text();
    QTableWidgetItem *n=ui->tableWidget->item(i,0);
    concat<< n->text();

    QString concating="";
    for(int i=0;i<data.size();i++){



    Instead of "JohnsmithjimmymitchelRusselArnoldMaryAnneShaunPaul" it prints "JohnsmithjohnjimmymitcheljimmyRusselArnoldRusselMaryAnneMaryShaunPaulShaun"(5 extra words) How can I correct this?

  • Lifetime Qt Champion


    You are concatenating twice the content of the same item. m and n are pointing to the same item. So from what you wrote you should remove the two lines handling that n object.

  • @SGaist Dman that was silly mistake :(