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. how to remove Item in Qtablewidget?
Forum Updated to NodeBB v4.3 + New Features

how to remove Item in Qtablewidget?

Scheduled Pinned Locked Moved Solved General and Desktop
qtablewidgetitemdeletefreememory
6 Posts 3 Posters 8.9k Views 1 Watching
  • 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.
  • F Offline
    F Offline
    ForestPoem
    wrote on 22 Dec 2015, 07:40 last edited by ForestPoem
    #1

    Hi. Guys!

    My source code ↓

    ui->tableWidget->setItem(0,0,new QTableWidgetItem(QString::number(3)));
    ui->tableWidget->item(0,0)->setTextAlignment(Qt::AlignCenter);
    My approach :

    delete ui->tableWidget->item(0,7);

    If this memory is free?

    If not, let me know any other method.

    R 1 Reply Last reply 22 Dec 2015, 07:43
    0
    • F ForestPoem
      22 Dec 2015, 07:40

      Hi. Guys!

      My source code ↓

      ui->tableWidget->setItem(0,0,new QTableWidgetItem(QString::number(3)));
      ui->tableWidget->item(0,0)->setTextAlignment(Qt::AlignCenter);
      My approach :

      delete ui->tableWidget->item(0,7);

      If this memory is free?

      If not, let me know any other method.

      R Offline
      R Offline
      Ratzz
      wrote on 22 Dec 2015, 07:43 last edited by Ratzz
      #2

      @ForestPoem
      Is this the same Question?

      --Alles ist gut.

      F 1 Reply Last reply 22 Dec 2015, 08:00
      1
      • R Ratzz
        22 Dec 2015, 07:43

        @ForestPoem
        Is this the same Question?

        F Offline
        F Offline
        ForestPoem
        wrote on 22 Dec 2015, 08:00 last edited by ForestPoem
        #3

        @Ratzz
        Thanks, but
        That's right, but that's the not answer.
        i'dont know item.

        I call the function each time the button clicked.

        connect(ui->tableWidget, SIGNAL(clicked()), this, SLOT(settarget()));

        void TargetSet::settarget(){
        for(int i=0;i<ui->tableWidget->rowCount();i++){
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(i));
        ui->tableWidget->item(i,0)->setTextAlignment(Qt::AlignCenter);
        }
        }

        When you do this, it will be a memory leak.
        For this reason, I would like to know the remove

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 22 Dec 2015, 23:50 last edited by
          #4

          Hi,

          Why not just update the text of your items ?

          Otherwise takeItem + delete on the item returned.

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

          F 1 Reply Last reply 23 Dec 2015, 00:31
          1
          • S SGaist
            22 Dec 2015, 23:50

            Hi,

            Why not just update the text of your items ?

            Otherwise takeItem + delete on the item returned.

            F Offline
            F Offline
            ForestPoem
            wrote on 23 Dec 2015, 00:31 last edited by ForestPoem
            #5

            @SGaist

            thanks, you say this?

            delete ui->tableWidget->takeItem(row,col);

            or

            ui->tableWidget->item(row,col)->setText("New text");

            This method had not thought of.

            Thank you!!

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 23 Dec 2015, 00:43 last edited by
              #6

              Using setText will avoid useless delete/allocation of items.

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

              1 Reply Last reply
              0

              4/6

              22 Dec 2015, 23:50

              • Login

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