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->setRowCount(5);
ui->tableWidget->setColumnCount(2); 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();
concat<<ui->tableWidget->item(i,1)->text();
QTableWidgetItem *n=ui->tableWidget->item(i,0);
concat<< n->text();QString concating="";
for(int i=0;i<data.size();i++){
concating+=data[i];}
ui->label->setText(concating);
}Instead of "JohnsmithjimmymitchelRusselArnoldMaryAnneShaunPaul" it prints "JohnsmithjohnjimmymitcheljimmyRusselArnoldRusselMaryAnneMaryShaunPaulShaun"(5 extra words) How can I correct this?
-
Hi,
You are concatenating twice the content of the same item.
m
andn
are pointing to the same item. So from what you wrote you should remove the two lines handling thatn
object.