Results 1 to 9 of 9

Thread: QtableWidget and numbers of row

  1. #1
    Join Date
    Dec 2010
    Posts
    24
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default QtableWidget and numbers of row

    I Have a qtableWidget and must delete all existing rows when click a button, I am trying as follows, but it only deletes one row at a time

    Qt Code:
    1. for (int i = 0; i < ui.tableWidget->rowCount(); i++)
    2. ui.tableWidget->removeRow(i);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QtableWidget and numbers of row

    Is this the real and full code?

    Post few lines before and after as well please.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: QtableWidget and numbers of row

    try "clear" (delete all the items) or "clearContents" (delete all the items but the headers)

  4. #4
    Join Date
    Dec 2010
    Posts
    24
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QtableWidget and numbers of row

    Quote Originally Posted by jano_alex_es View Post
    try "clear" (delete all the items) or "clearContents" (delete all the items but the headers)
    I had tried "clearContents" but it clears the contents, leaving lines in the table, I need to remove the lines also

    thanks

  5. #5
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: QtableWidget and numbers of row

    So try "clear"... as far as I remember, it deletes all, even the lines.

  6. The following user says thank you to jano_alex_es for this useful post:

    VitaliBR (3rd February 2011)

  7. #6
    Join Date
    Dec 2010
    Posts
    24
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QtableWidget and numbers of row

    Quote Originally Posted by jano_alex_es View Post
    So try "clear"... as far as I remember, it deletes all, even the lines.
    It erases all the same, including the column names
    there is no other way?

    Or how do I put the names in the columns again? (I named the columns using the QTDesigner)

    lol

    Thanks


    I tried this, but the program simply fails (crash) if I try to rename the columns
    Qt Code:
    1. ui.tableWidget->clear();
    2.  
    3. ui.tableWidget->horizontalHeaderItem(0)->setText("Name");
    4. ui.tableWidget->horizontalHeaderItem(1)->setText("IP/DNS");
    5. ui.tableWidget->horizontalHeaderItem(2)->setText("Phone");
    6. ui.tableWidget->horizontalHeaderItem(3)->setText("Situation");
    To copy to clipboard, switch view to plain text mode 
    Last edited by VitaliBR; 3rd February 2011 at 19:02.

  8. #7
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: QtableWidget and numbers of row

    I think you delete the headers when you use "clear", so you need to create them again.

    Something like:
    Qt Code:
    1. QTableWidgetItem* pHeader1 = new QTableWidgetItem("Name");
    2. ui.tableWidget->setHorizontalHeaderItem(0, pHeader1);
    3. QTableWidgetItem* pHeader2 = new QTableWidgetItem("IP/DNS");
    4. ui.tableWidget->setHorizontalHeaderItem(1, pHeader2);
    5. //etc
    To copy to clipboard, switch view to plain text mode 

  9. #8
    Join Date
    Dec 2010
    Posts
    24
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QtableWidget and numbers of row

    I solved my problem:

    Qt Code:
    1. for(int i = ui.tableWidget->rowCount() - 1; i >= 0; i--){
    2. ui.tableWidget->removeRow(i);
    3. }
    To copy to clipboard, switch view to plain text mode 

    Thanks

  10. #9
    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: QtableWidget and numbers of row

    QTableWidget::setRowCount() will remove rows to decrease the row count. Set it to zero...

Similar Threads

  1. How to sort QListWidget by numbers...
    By Patrick Sorcery in forum Newbie
    Replies: 7
    Last Post: 6th November 2017, 19:39
  2. Replies: 1
    Last Post: 3rd January 2011, 12:42
  3. Replies: 1
    Last Post: 6th July 2010, 16:40
  4. Complex Numbers
    By Max Yaffe in forum Qt Programming
    Replies: 2
    Last Post: 24th May 2007, 18:40
  5. Column with numbers
    By ederbs in forum Qt Programming
    Replies: 1
    Last Post: 29th November 2006, 23:03

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.