Results 1 to 2 of 2

Thread: QTableWidget sorting

  1. #1
    Join Date
    Apr 2011
    Posts
    132
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTableWidget sorting

    QTableWidget

    Qt Code:
    1. this->setColumnCount(4);
    2. this->setSortingEnabled(true);
    To copy to clipboard, switch view to plain text mode 

    then display my rows... works fine.
    Now when I add

    Qt Code:
    1. this->sortItems( 1, Qt::AscendingOrder );
    To copy to clipboard, switch view to plain text mode 

    I have garbage in rows, basically no data in 2 and 4 columns.

    Same when I remove to sortItems()... and click on header to sort the column

    have U seen that before ?

    I use setItem() to insert it.

    Please help

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QTableWidget sorting

    You need to disable sorting while you are setting / inserting the items.
    Qt Code:
    1. ...
    2. this->setSortingEnabled(false);
    3. this->setItem(...);
    4. this->setSortingEnabled(true);
    5. ...
    To copy to clipboard, switch view to plain text mode 
    This is because as soon as you set/insert an item, table will automatically sort again and the just inserted item will be moved to some new row, but you will still try use the old row number, and things get messed up and your program may even crash.

Similar Threads

  1. QTableWidget sorting
    By abrou in forum Newbie
    Replies: 2
    Last Post: 6th June 2012, 10:53
  2. QTableWidget and sorting like int
    By TomASS in forum Newbie
    Replies: 3
    Last Post: 18th February 2010, 14:54
  3. Sorting a qtablewidget......
    By reshma in forum Qt Programming
    Replies: 8
    Last Post: 23rd March 2009, 14:34
  4. QTableWidget sorting
    By losiem in forum Qt Programming
    Replies: 4
    Last Post: 14th July 2008, 20:20
  5. QTableWidget Sorting
    By mclark in forum Newbie
    Replies: 4
    Last Post: 29th September 2006, 22:34

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.