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. Adding text from line edits to tablewidget

Adding text from line edits to tablewidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.4k 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 last edited by
    #1

    The following code is used by me to add text in several lineedits to a tablewidget when button is clicked! Though I am able to add the values to a single row when I fill the lineedits and click the button again the previous row is overridden without getitng a new row

    void MainWindow::on_pushButton_2_clicked()
    {
    int m=1
    ui->tableWidget_2->setColumnCount(3);
    ui->tableWidget_2->setRowCount(m);

    ui->tableWidget_2->setItem(m-1, 0, new QTableWidgetItem(ui->lineEdit->text()));
    ui->tableWidget_2->setItem(m-1, 1, new QTableWidgetItem(ui->lineEdit_1->text()));
    ui->tableWidget_2->setItem(m-1, 2, new QTableWidgetItem(ui->lineEdit_2->text()));
    m++;

    }
    here i increment m(number of rows) by 1 inorder to add a new row! But no new row is created only the previous row is overridden! How can I prevent this so that I can add multiplerows?

    1 Reply Last reply
    0
    • Pradeep KumarP Offline
      Pradeep KumarP Offline
      Pradeep Kumar
      wrote on last edited by
      #2

      Hi,

      When u click on button, u will get one row based on condition.
      m = 1;
      then m value is changed to 2, where the statements wont execute further to add new row.

      Thanks,

      Pradeep Kumar
      Qt,QML Developer

      1 Reply Last reply
      3
      • L Offline
        L Offline
        Lasith
        wrote on last edited by
        #3

        yeah so how can I alter the code to add a new row?

        jsulmJ 1 Reply Last reply
        0
        • L Lasith

          yeah so how can I alter the code to add a new row?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Lasith
          What is the point to increment a local variable m (m++;)? It is local and will be initialized to 1 next time on_pushButton_2_clicked() is called.
          Use http://doc.qt.io/qt-5/qtablewidget.html#rowCount to get current number of rows and add 1:
          void MainWindow::on_pushButton_2_clicked()
          {
          int m= ui->tableWidget_2->rowCount() + 1;
          ui->tableWidget_2->setColumnCount(3);
          ui->tableWidget_2->setRowCount(m);

          ui->tableWidget_2->setItem(m-1, 0, new QTableWidgetItem(ui->lineEdit->text()));
          ui->tableWidget_2->setItem(m-1, 1, new QTableWidgetItem(ui->lineEdit_1->text()));
          ui->tableWidget_2->setItem(m-1, 2, new QTableWidgetItem(ui->lineEdit_2->text()));
          }

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          4
          • L Offline
            L Offline
            Lasith
            wrote on last edited by
            #5

            Thanx alot this works

            1 Reply Last reply
            1

            • Login

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