Results 1 to 10 of 10

Thread: removing row from QTableWidget, showing no effect

  1. #1
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default removing row from QTableWidget, showing no effect

    Hello all,
    I'm trying to remove item from QTable widget as shown below....
    Qt Code:
    1. for(int m=NewRow;m<ui->tableWidget_ouput->rowCount();m++)
    2. {
    3.  
    4. ui->tableWidget_ouput->removeRow(m);
    5. }
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: removing row from QTableWidget, showing no effect

    Its ok...
    This problem is solved...!

  3. #3
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: removing row from QTableWidget, showing no effect

    How did you solve it?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: removing row from QTableWidget, showing no effect

    When you remove row x then old row x + 1 becomes the new row x. If you then increment x, as happens in the for loop, you skip a row.
    You can see the behaviour in the following example:
    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char **argv) {
    5. QCoreApplication app(argc, argv);
    6.  
    7. QList<int> l;
    8. l << 11 << 12 << 13 << 14 << 15 << 16 << 17 << 18 << 19;
    9.  
    10. qDebug() << "Start" << l;
    11. for (int r = 2; r < l.size(); ++r) {
    12. qDebug() << "r =" << r << "removing" << l.at(r);
    13. l.removeAt(r);
    14. qDebug() << "Remaining" << l;
    15. }
    16.  
    17. return 0;
    18. }
    To copy to clipboard, switch view to plain text mode 

    Either remove the rows in reverse order, or determine the number of rows to remove and remove the first row that number of times. If you are trying to empty an entire QList use clear(). If you are trying to empty an entire QTableWidget set the row count to zero.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: removing row from QTableWidget, showing no effect

    Quote Originally Posted by ChrisW67 View Post
    When you remove row x then old row x + 1 becomes the new row x. If you then increment x, as happens in the for loop, you skip a row.
    Moreover, removing a row decreases the rowCount() of the table which is what your stop condition depends on:
    Qt Code:
    1. for(int m=NewRow;m<ui->tableWidget_ouput->rowCount();m++)
    To copy to clipboard, switch view to plain text mode 
    thus you can effectively remove only half of the rows (so every 2nd of the first 75% of the rows in your case).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: removing row from QTableWidget, showing no effect

    I used backword loop, removing items from last,
    As shown below
    Qt Code:
    1. int m=ui->tableWidget_ouput->rowCount();
    2. while(m>=NewRow)
    3. {
    4. ui->tableWidget_ouput->removeRow(m);
    5. m--;
    6. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: removing row from QTableWidget, showing no effect

    Ehm, what about QTableWidget::clearContents() or QTableWidget::clear()?

    EDIT: Forget it, read too fast, didn't see NewRow...

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: removing row from QTableWidget, showing no effect

    I went there too. Those functions remove the content from the table but leave the dimensions the same, i.e. rowCount() is unchanged. Contrast that with removeRow(), as used in the original post, which actually shrinks the table. I guess it really depends on what your application actually requires.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: removing row from QTableWidget, showing no effect

    Wouldn't this be the quickest?

    Qt Code:
    1. ui->tableWidget->setRowCount(NewRow);
    To copy to clipboard, switch view to plain text mode 

    I don't know what happens with those excessive items, if they are deleted or not.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: removing row from QTableWidget, showing no effect

    Because I was curious:
    Qt Code:
    1. void QTableModel::setRowCount(int rows)
    2. {
    3. int rc = verticalHeaderItems.count();
    4. if (rows < 0 || rc == rows)
    5. return;
    6. if (rc < rows)
    7. insertRows(qMax(rc, 0), rows - rc);
    8. else
    9. removeRows(qMax(rows, 0), rc - rows);
    10. }
    To copy to clipboard, switch view to plain text mode 
    Yes, they are!

Similar Threads

  1. Replies: 1
    Last Post: 5th May 2011, 09:37
  2. Removing pressed effect from a QPushButton
    By Luc4 in forum Qt Programming
    Replies: 3
    Last Post: 14th August 2010, 11:43
  3. Removing Focus from QTableWidget
    By kapoorsudhish in forum Newbie
    Replies: 2
    Last Post: 22nd October 2009, 14:16
  4. Removing items from QtableWidget
    By algajard in forum Qt Programming
    Replies: 1
    Last Post: 17th March 2009, 09:31
  5. Removing a QWidget object from a QTableWidget cell
    By mclark in forum Qt Programming
    Replies: 5
    Last Post: 13th March 2008, 15:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.