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. Removing row from QTableWidget, showing no effect
Forum Updated to NodeBB v4.3 + New Features

Removing row from QTableWidget, showing no effect

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 4.1k 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.
  • A Offline
    A Offline
    aurora
    wrote on last edited by
    #1

    Hello all,
    I'm trying to remove item from QTable widget as shown below....
    @

    for(int m=NewRow;m<ui->tableWidget_ouput->rowCount();m++)
    {
     
    ui->tableWidget_ouput->removeRow(m);
    }
    

    @

    But its effect is not displaying i.e, the removed rows still present in the table....Please tell me , what i'm missing here?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Judging from the code, you were looking for the QTableWidget::clear() method?

      1 Reply Last reply
      0
      • A Offline
        A Offline
        aurora
        wrote on last edited by
        #3

        [quote author="Andre" date="1326356389"]Judging from the code, you were looking for the QTableWidget::clear() method?[/quote]

        Not just clearing those rows, i want to remove those rows from table....

        1 Reply Last reply
        0
        • K Offline
          K Offline
          KA51O
          wrote on last edited by
          #4

          Since the table gets its data from the model I'd say remove them from the model.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            [quote author="KA51O" date="1326361061"]Since the table gets its data from the model I'd say remove them from the model.[/quote]

            QTableWidget provides its own, private model, and a separate API to manipulate the items in that model, so this does not work for QTableWidget.

            [quote author="aurora" date="1326359238"]
            Not just clearing those rows, i want to remove those rows from table....[/quote]
            OK, if you just want to delete specific rows, you should indeed use removeRow. Note however that your loop is wrong. Let's work through it step by step:

            You start with row number NewRow and assing that to m

            You remove this row m

            Now, row m + 1 moved up in the table, and thus becomes the new row number m

            You increment m

            You compare m with the rowcount

            if m is smaller then rowcount, you continue with step 2

            else you exit your loop.

            Step 3 is crusial, and probably not what you expected. Because after removing row m, you increment m, you are not going to remove the row that moved to row m after your deletion!

            There are two simple ways out of this problem. The general solution for avoiding issues with chaning indexes in lists while iterating over such a list, is to iterate backwards: from the last item forward. If you remove an item while iterating backwards, only indexes of items that you have already iterated over will change, and you don't care about these. In your case, you can also simply not increment m in your loop. That works because you remove all rows after row NewRow, and thus rowCount() will drop to m.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              aurora
              wrote on last edited by
              #6

              Oh....Thanks a lot Andre....
              Ya i tried backword loop....it works fine now....:)
              Thank u so much..!!!

              1 Reply Last reply
              0

              • Login

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