Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. concatanating qtablewidget items into string incorrect
QtWS25 Last Chance

concatanating qtablewidget items into string incorrect

Scheduled Pinned Locked Moved Solved General and Desktop
qtcreatorqstringlistqstringconcatanation
3 Posts 2 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lasith
    wrote on 2 Oct 2017, 18:18 last edited by Lasith 10 Feb 2017, 18:21
    #1

    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?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 2 Oct 2017, 19:53 last edited by
      #2

      Hi,

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply 3 Oct 2017, 01:40
      1
      • S SGaist
        2 Oct 2017, 19:53

        Hi,

        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.

        L Offline
        L Offline
        Lasith
        wrote on 3 Oct 2017, 01:40 last edited by
        #3

        @SGaist Dman that was silly mistake :(

        1 Reply Last reply
        0

        1/3

        2 Oct 2017, 18:18

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved