+ Reply to Thread
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,317
    Thanks
    20
    Thanked 377 Times in 371 Posts
    Qt products
    Qt3 Qt4
    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.
    Recommended off-line reading:
    The Foundations of Qt Development (ISBN: 1-59059-831-8)

  3. #3
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 18 Times in 18 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 18 Times in 18 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 18 Times in 18 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
    6,442
    Thanks
    13
    Thanked 1,359 Times in 1,301 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...

+ Reply to Thread

Similar Threads

  1. How to sort QListWidget by numbers...
    By Patrick Sorcery in forum Newbie
    Replies: 5
    Last Post: 7th December 2011, 04:05
  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